Data Structures & AlgorithmsHardTCS NQT Digital · Product Company SDE

Problem: Counting sort is applied to [3, 1, 2, 3, 0, 1] with keys in the range 0 to 3. After counting occurrences and computing prefix sums, the stable output array is built. What is the position (0-based) of the first occurrence of the value 3 in the output?

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

Correct answer: C. 4

The output positions of a value start after all smaller values; there are 1 + 2 + 1 = 4 elements smaller than 3 (values 0, 1, 1 and 2), so the first 3 lands at index 4.

More Coding Interview Problems questions

Exams that ask this topic