Finding Median in a Data Stream
Published: Sun Feb 15 2026 | Modified: Sat Feb 07 2026 , 3 minutes reading.
Finding Median in a Data Stream (Two Heaps)
Introduction: The “Middle Man” Problem
Finding the median of a sorted array is easy (). But what if the data is a stream?
- Sort Every Time: per new item. Too slow.
- Maintain Sorted Array: to shift elements per insertion. Still too slow for millions of points.
The Two Heaps pattern allows you to find the median in time and add new elements in time. It’s the gold standard for real-time statistical monitoring.
What Problem does it solve?
- Input: A continuous stream of numbers.
- Output: The current median at any moment.
- The Promise: Balanced performance between insertion and query.
Core Idea (Intuition)
Divide the data into two halves:
- Lower Half: Store the smaller half of the data in a Max-Heap. The largest of the small numbers is at the top.
- Upper Half: Store the larger half of the data in a Min-Heap. The smallest of the large numbers is at the top.
The Median is always either the top of one of these heaps (if total items are odd) or the average of the two tops (if total items are even).
How it Works
- Balance: Keep the sizes of the two heaps nearly equal (difference ).
- Insert:
- Add the new number to the appropriate heap.
- If the heaps become unbalanced, “move” the top element from the larger heap to the smaller one.
- Find Median:
- If total items are odd, return the top of the larger heap.
- If even, return
(MaxHeap.top + MinHeap.top) / 2.
Typical Business Scenarios
- ✅ Performance Analysis: Calculating the “Median Response Time” of a web service as requests flow in.
- ✅ Financial Tech: Real-time median price calculation for assets in a volatile market.
- ✅ Interactive Data Apps: Dashboards that update stats as the user uploads data.
Performance & Complexity
- Add Item: .
- Find Median: .
- Space: to store all items.
Summary
"The Two Heaps pattern is like a balanced scale. By splitting the data into two opposing heaps, it keeps the 'Middle Man' (the median) right at the top where you can see it instantly."
