Data Structures & AlgorithmsVery hardProduct Company SDE · ICPC regional

Problem: The stream of numbers 3, 8, 2, 9, 4, 7, 6, 1 is processed by a running median: after each number, the median of the numbers so far is recorded (for an even count, use the lower of the two middle values). What is the sum of the recorded medians?

  1. A. 45
  2. B. 37.5
  3. C. 26
  4. D. 30

Correct answer: D. 30

Two heaps give the lower medians 3, 3, 3, 3, 4, 4, 6, 4, summing to 30. Taking the upper middle for even counts gives 45, averaging the two middles gives 37.5, and missing the median after the final value gives 26.

More Coding Interview Problems questions

Exams that ask this topic