Data Structures & AlgorithmsModerateTCS NQT Digital · Product Company SDE

Problem: In the array [2, 9, 4, 7, 1, 8], find the 4th smallest element using the quickselect idea (its final answer is the value that would sit at sorted index 3, 0-based).

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

Correct answer: C. 7

Sorted, the array is [1, 2, 4, 7, 8, 9], so sorted index 3 holds 7, which quickselect returns. Treating 4th as index 4 gives 8, the 4th largest is 4, and returning the first pivot unpartitioned gives 2.

More Coding Interview Problems questions

Exams that ask this topic