Data Structures & AlgorithmsVery hardProduct company OA

Running median with two heaps (a max-heap for the lower half, a min-heap for the upper half, sizes differing by at most 1, the lower half keeping the extra element). After the stream 5, 15, 1, 3, 8, 7, what are the two heap tops and the median?

  1. A. lower top 5, upper top 8, median 6.5
  2. B. lower top 3, upper top 7, median 5.0
  3. C. lower top 5, upper top 7, median 6.0
  4. D. lower top 7, upper top 8, median 7.5

Correct answer: C. lower top 5, upper top 7, median 6.0

Sorted stream: 1, 3, 5, 7, 8, 15. The lower half {1, 3, 5} has max 5 and the upper half {7, 8, 15} has min 7, so the median is (5 + 7) / 2 = 6.0. Reporting the middle of the arrival order or forgetting to rebalance after 7 arrives gives the other pairs.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic