Data Structures & AlgorithmsHardProduct company OA

Quickselect with Lomuto partition (pivot = last element) looks for the 3rd smallest element of [7, 2, 9, 4, 3, 8], recursing only into the side that contains the target. How many partition calls are made?

  1. A. 1
  2. B. 2
  3. C. 4
  4. D. 3

Correct answer: C. 4

Target index k = 2. Call 1 (whole array, pivot 8) puts 8 at index 4 → array [7, 2, 4, 3, 8, 9] → go left. Call 2 on indices 0–3 (pivot 3) puts 3 at index 1 → [2, 3, 4, 7, …] → go right. Call 3 on indices 2–3 (pivot 7) puts 7 at index 3 → go left. Call 4 on the one-element range 2–2 puts 4 at index 2 = k. Answer 4 with 4 partition calls; stopping the count when the range first contains the answer gives 3.

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

Exams that ask this topic