Data Structures & AlgorithmsModerateTCS NQT Digital · Infosys

Problem: The array [6, 1, 9, 4, 4, 8] is scanned, and for each index the number of earlier elements that are strictly smaller is recorded. What is the sum of these counts?

  1. A. 9
  2. B. 8
  3. C. 6
  4. D. 4

Correct answer: B. 8

Counts per index are 0, 0, 2, 1, 1, 4, summing to 8. Using ≤ counts the equal 4 as well for 9, counting earlier larger elements gives 6, and counting the indices with a non-zero count gives 4.

More Coding Interview Problems questions

Exams that ask this topic