Two heaps, 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) to maintain the median in O(log n) per insertion and O(1) for getMedian. Explain the balancing logic and how to handle even/odd total counts. Analyze time and space complexity, and discuss trade-offs versus other approaches like sorted arrays or balanced BSTs.
Pro tip: Mention that this two-heap pattern is a classic streaming median solution and is directly applicable to online learning scenarios where data arrives sequentially. Also, note that Python's heapq is a min-heap, so you can simulate a max-heap by negating values.
Confirm that add(x) can be called multiple times and getMedian() may be called after each insertion. Ask about expected data volume and whether duplicates are allowed.
Describe maintaining a max-heap for the lower half and a min-heap for the upper half. Explain that the heaps are kept balanced such that their sizes differ by at most one.
Outline the steps: add to max-heap, move the largest from max-heap to min-heap, then if min-heap is larger, move the smallest back to max-heap. This ensures all elements in max-heap are <= all elements in min-heap.
If total count is odd, the median is the top of the larger heap; if even, it's the average of the tops of both heaps.
State that add(x) takes O(log n) time due to heap operations, getMedian() takes O(1), and space is O(n). Compare with alternatives like sorted list (O(n) insertion) or balanced BST (O(log n) but more complex).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.
Explain that with the full input available upfront, you can use a selection algorithm like Quickselect to find the median in O(n) average time with O(1) auxiliary space, or sort the array in-place if O(n log n) time is acceptable. Emphasize that this avoids the O(n) auxiliary space of the two-heap approach while still achieving linear expected time.
Pro tip: Mention that Quickselect can be made deterministic with the Median of Medians algorithm for O(n) worst-case time, but in practice, randomized Quickselect is often preferred for its simplicity and speed. Also, note that for ML applications, if the data is already sorted or nearly sorted, a simple in-place sort might be more efficient.
Confirm that the full input is available in memory and that auxiliary space should be minimized. Discuss whether the input can be modified (in-place) and whether worst-case or average-case time complexity is more important.
Describe the Quickselect algorithm: partition the array around a pivot, then recursively search only the side containing the median. This yields O(n) average time and O(1) auxiliary space.
Explain that random pivot selection gives expected O(n) time, while Median of Medians guarantees O(n) worst-case time. Mention trade-offs: randomized is simpler and faster in practice, deterministic has higher constant factors.
Note that sorting the array in-place (e.g., heapsort) also uses O(1) auxiliary space but takes O(n log n) time. For median finding, Quickselect is asymptotically faster on average.
Connect to ML scenarios: median is used for robust statistics, outlier detection, or quantile computation. Emphasize that memory efficiency matters for large datasets, and in-place algorithms are valuable when data fits in memory but auxiliary space is limited.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.