← Trexquant Interview Insights
Classic two-heap setup, one max-heap for the lower half and one min-heap for the upper half.
Use two heaps: a max-heap for the lower half and a min-heap for the upper half, keeping their sizes balanced. For each incoming integer, insert into the appropriate heap and rebalance so the size difference is at most one. The median is then either the top of the larger heap or the average of the two tops.
Pro tip: Mention that this approach gives O(log n) insertion and O(1) median query, and discuss how to handle duplicates and integer overflow when averaging. Also, briefly compare with alternatives like a self-balancing BST or order-statistic tree to show depth.
Confirm that the data structure should support addNum(int) and findMedian() operations, and discuss expected time complexity and memory constraints.
Select two heaps: a max-heap for the lower half and a min-heap for the upper half. Explain why heaps are ideal for maintaining the median dynamically.
Describe the algorithm: add to max-heap if num <= max-heap top, else to min-heap; then rebalance by moving the top element from the larger heap to the other if sizes differ by more than one.
If heaps are equal size, median is the average of the two tops; otherwise, it's the top of the larger heap. Discuss handling of integer division and overflow.
State time complexities: O(log n) for insertion, O(1) for median. Mention edge cases: empty stream, single element, duplicates, and large numbers.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.