← Trexquant Interview Insights
My first instinct was to just keep a sorted list and binary search insert, which is technically O(log n) to find the position but O(n) to shift elements.
Start by clarifying the requirements: median definition for even/odd counts, data types, and whether the stream can be replayed. Then propose a two-heap solution (max-heap for lower half, min-heap for upper half) with balanced sizes, explaining how insertion maintains balance in O(log n) and median retrieval is O(1).
Pro tip: Mention that the two-heap approach is the standard optimal solution, but also discuss edge cases like empty stream, duplicates, and negative numbers to show thoroughness. If asked about trade-offs, note that a balanced BST or order-statistic tree also works but is more complex to implement.
Ask about median definition (average of two middle elements for even count), input size, memory limits, and whether the data structure needs to support deletions or only insertions.
Explain using a max-heap for the lower half and a min-heap for the upper half. The heaps are kept balanced such that their sizes differ by at most 1.
Describe how to insert a new number: add to one heap, then rebalance by moving the top element to the other heap if size difference exceeds 1. This ensures O(log n) time.
If heaps are equal size, median is the average of the two tops; otherwise, it's the top of the larger heap. This is O(1) time.
State time complexities: O(log n) insertion, O(1) median. Mention alternative structures like balanced BST with order statistics, but note the two-heap solution is simpler and efficient.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.