Data Structures & AlgorithmsHardTCS NQT Digital · Product Company SDE

Problem: A stream of integers arrives: 4, 9, 1, 7, 8, 3, 6. After each arrival from the 2nd onwards, we report the 2nd largest value seen so far (it is undefined after the first arrival). What is the sum of the reported values?

  1. A. 54
  2. B. 43
  3. C. 39
  4. D. 27

Correct answer: C. 39

Tracking the top two values, the reports are 4, 4, 7, 8, 8, 8, summing to 39. Reporting the maximum instead gives 6 × 9 = 54, also counting the first arrival 4 gives 43, and tracking the 2nd smallest gives 27.

More Coding Interview Problems questions

Exams that ask this topic