← Bloomberg Interview Insights
Use two heaps—a max-heap for the lower half and a min-heap for the upper half—to maintain the median in O(log n) insertion and O(1) retrieval. Explain the balancing logic and how to handle even and odd total counts.
Pro tip: Mention that this two-heap approach is the standard solution for streaming medians and is used in real-time systems like Bloomberg's market data feeds, where low-latency median calculation is critical.
Ask about the expected frequency of median queries, memory limits, and whether the stream is truly unbounded. This shows you consider practical trade-offs.
Describe using a max-heap for the lower half and a min-heap for the upper half. Explain that the heaps are kept balanced so their sizes differ by at most one.
Walk through adding a number: push to the appropriate heap, then rebalance by moving the top element if sizes differ by more than one. Emphasize O(log n) time.
If both heaps are equal size, median is the average of their tops; otherwise, it's the top of the larger heap. This is O(1) time.
State time and space complexity, and discuss handling empty stream, single element, and duplicate values. Optionally mention alternatives like balanced BSTs.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.