Data Structures & AlgorithmsModerateTCS NQT · Infosys pattern

Counting sort on [2, 0, 2, 1, 0, 2] (values 0–2) builds count[v] and then turns it into cumulative counts, count[v] = number of elements ≤ v. What is the cumulative count array?

  1. A. [2, 3, 6]
  2. B. [2, 1, 3]
  3. C. [0, 2, 3]
  4. D. [2, 3, 5]

Correct answer: A. [2, 3, 6]

Frequencies are 0: 2, 1: 1, 2: 3 → [2, 1, 3]. Running sums give [2, 3, 6]; the last entry always equals n = 6. Each cumulative value is one past the final position of that value, which is what makes the stable placement pass work. [0, 2, 3] is the exclusive (starting-index) version.

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

Exams that ask this topic