Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: The array [4, 1, 3, 2, 5] is processed by counting sort on the values 1 to 5, writing the values into an output array in sorted order. How many positions of the output array hold a value that equals its own index plus one (1-based position equals value)?

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

Correct answer: D. 5

After sorting, the output is 1 through 5 in order, so every position satisfies position equals value. The count is the array length, which is a quick sanity check for counting sort.

More Coding Interview Problems questions

Exams that ask this topic