Data Structures & AlgorithmsHardProduct Company SDE · Amazon OA

Problem: Using a min-heap, the stream 9, 4, 7, 1, 8, 2, 6, 3, 5 is processed as follows: after each insertion, pop the minimum if the heap then holds more than 3 values. After the whole stream, the heap holds 3 values. What is the sum of those 3 values?

  1. A. 17
  2. B. 6
  3. C. 24
  4. D. 21

Correct answer: C. 24

Popping the minimum whenever the heap exceeds 3 keeps the three largest values, 9 + 8 + 7 = 24. Popping when the size reaches 3 keeps only two (17), a max-heap keeps the three smallest (1 + 2 + 3 = 6), and summing the popped values gives 21.

More Coding Interview Problems questions

Exams that ask this topic