Find Median from Data Stream
A hard Heap problem included in Love Babbar 450, Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.
- Topic
- Heap
- Sheets
- 2
- Core for
- 9 roles
- Platform
- LeetCode
The problem
The median is the middle value in an ordered integer list. If the list size is even, the median is the average of the two middle values. Design a data structure that supports adding integers from a data stream and finding the current median of all elements added so far.
Example 1
- Input
- addNum(1), addNum(2), findMedian() -> 1.5, addNum(3), findMedian() -> 2
- Output
- [null, null, 1.5, null, 2]
- Why
- After adding 1 and 2, the sorted list is [1,2] and median is (1+2)/2 = 1.5. After adding 3, the sorted list is [1,2,3] and median is 2.
Example 2
- Input
- addNum(1), findMedian() -> 1, addNum(2), findMedian() -> 1.5
- Output
- [null, 1, null, 1.5]
- Why
- After adding just 1, the median is 1. After adding 2, sorted list is [1,2], median is 1.5.
Constraints
- -10^5 <= num <= 10^5
- At most 5 * 10^4 calls will be made to addNum and findMedian
How to think about it
Updated 2026-09-09The median divides numbers into two halves: smaller numbers and larger numbers. You never care about the internal order within either half, only the boundary where they touch. Putting the lower half into a max-heap and the upper half into a min-heap places both middle candidates directly at the roots.
Approaches, worst first
Insertion sort into dynamic array
time O(n) · space O(n)
Maintain a sorted array. Use binary search to locate the correct insertion index for the incoming number, then insert it. Finding the median takes O(1), but shifting elements on insertion burns linear time on every add, so the add dominates.
Two balancing heapsWrite this one
time O(log n) · space O(n)
Use a max-heap for the lower half and a min-heap for the upper half. Route the incoming value through the heaps and rebalance sizes so their lengths differ by at most one. The median is either the top of the larger heap or the average of both tops, so the add dominates and finding the median is free.
Where people lose marks · 3
- Returning integer division when averaging the two middle numbers for an even total count. 1 and 2 must produce 1.5, so floating-point division is mandatory.
- Letting the two heaps invert their order. Never insert directly into one heap without checking whether the new value actually belongs on the other side of the median boundary.
- Allowing heap sizes to drift apart by more than one element. Balancing must happen on every single addNum call, not deferred until findMedian.
The theory behind it
Heap — the ground this problem stands on. All Heap problems
What Heap is
A heap is a specialized tree that keeps only the single most extreme item at the very top. In a min-heap, every parent node is smaller than its children, so the smallest element in the entire collection sits immediately at the root. Unlike a binary search tree, a heap does not keep all items in full sorted order. It maintains only a partial order, making it fast at giving you the single smallest or largest item without spending time sorting everything else.
When to reach for it
Reach for a heap when a problem asks for the top k largest elements, the kth smallest value, or a running median from a stream of numbers. Signals include phrases like continuously finding the cheapest item, merging k sorted linked lists, or scheduling tasks with priorities. Whenever you need repeated access to the minimum or maximum value while items are added and removed dynamically, a priority heap is the tool.
How the pattern works
To find the k largest elements, keep a min-heap of fixed size k. Push incoming numbers into the heap; whenever the heap size grows past k, pop the top item, which is the smallest among them. After processing all elements, only the k largest remain. For a running median, balance two heaps: a max-heap holding the smaller half of numbers and a min-heap holding the larger half. In code, heaps are stored compactly as flat arrays where a node at index i has children at indices 2i plus 1 and 2i plus 2.
What each operation costs
| Operation | Time |
|---|---|
| read the minimum or maximum element | O(1) |
| insert a new element and sift into position | O(log n) |
| remove the top element and sift down | O(log n) |
| build a heap from an array of n items | O(n) |
What usually goes wrong with Heap
- Using a max-heap instead of a min-heap when keeping the k largest elements, causing the largest values to be evicted while small items stay behind.
- Assuming that extracting elements by iterating over the backing array yields sorted order, without popping items from the heap one by one.
- Forgetting that standard language libraries provide a min-heap by default, leading to wrong answers when a max-heap was required.
Which roles need this problem
Heap is a core topic for these 9 roles — if you're targeting one of them, this problem is early in your path, not optional.
Secondary for 11 more roles, including SDE / Backend Engineer, Data Engineer, ML Engineer.
Track this in your role's order
Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.
Start freeMore Heap problems
Problem set and role mapping as of .