Data Structures & AlgorithmsModerateProduct placement · Amazon/Microsoft

Quickselect returns the k-th smallest element of an unsorted array in expected O(n) time. For [14, 4, 8, 46, 16, 34, 31, 40] and k = 3, what does it return, and how many elements does the first Lomuto partition (pivot = last element) place to the LEFT of the pivot?

  1. A. 34; 6
  2. B. 14; 6
  3. C. 14; 1
  4. D. 16; 6

Correct answer: B. 14; 6

The k-th smallest of [4, 8, 14, 16, 31, 34, 40, 46] is 14. The first partition around 40 puts the 6 smaller elements to its left; quickselect then recurses into only one side.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic