Data Structures & AlgorithmsVery hardProduct Company SDE · Infosys OA

Problem: A max-heap starts with the values 9, 8, 6, 5, 4, 3, 1 (inserted in that order). Then three times in succession: pop the maximum, and push half of the popped value rounded down. After these three rounds, what is the sum of all values remaining in the heap?

  1. A. 35
  2. B. 25
  3. C. 24
  4. D. 13

Correct answer: C. 24

Round 1 pops 9 and pushes 4, round 2 pops 8 and pushes 4, round 3 pops 6 and pushes 3, leaving 5, 4, 4, 4, 3, 3, 1 = 24. A min-heap pops 1, 0 and 0 instead (35), rounding up pushes 5, 4, 3 (25), and popping without pushing back leaves 13.

More Coding Interview Problems questions

Exams that ask this topic