Find Median from Data Stream
Difficulty: Hard
Design a class that takes numbers one at a time and can report the median of all numbers so far.
The median is the middle value of a sorted list. With an odd count, it's the middle value. With an even count, it's the average of the two middle values.
addNum(num) adds a number, and findMedian() returns the median of every number added so far.
Examples
Input: addNum(1); addNum(2); findMedian()
Output: 1.5
The numbers so far are [1,2]. With an even count, the median is the average of both middle values: (1+2)/2 = 1.5.
Input: addNum(3); findMedian()
Output: 2
The numbers so far are [1,2,3]. With an odd count, the median is the single middle value, which is 2.
Constraints
-10^5 <= num <= 10^5
At most 5 * 10^4 calls total to addNum and findMedian
Approach
A sorted array works, but inserting into the middle means shifting elements. And you never need the whole array sorted, only what's near the middle.
Split the stream into two halves:
- A max-heap holds the smaller half. Its top is the number just below the middle.
- A min-heap holds the larger half. Its top is the number just above the middle.
Keep the heaps balanced in size (equal, or the max-heap one larger), and the median can always be read from their tops.
Each new number goes into the half it belongs to, then the heaps are rebalanced if needed.
Solutions
Brute Force - Sorted Array with Insertion
Keep all numbers in a sorted array. addNum finds the insertion point and splices the number in. findMedian reads the middle position(s).
class MedianFinder {
constructor() {
this.sorted = [];
}
addNum(num) {
let lo = 0;
let hi = this.sorted.length;
while (lo < hi) {
const mid = (lo + hi) >> 1;
if (this.sorted[mid] < num) lo = mid + 1;
else hi = mid;
}
this.sorted.splice(lo, 0, num);
}
findMedian() {
const n = this.sorted.length;
const mid = n >> 1;
if (n % 2 === 1) return this.sorted[mid];
return (this.sorted[mid - 1] + this.sorted[mid]) / 2;
}
}Time: O(n) per addNum (binary search is O(log n), but splice's shift is O(n)); O(1) per findMedian · Space: O(n)
Optimal - Two Heaps
Split the stream across a max-heap (smaller half) and a min-heap (larger half), kept balanced in size. Read the median from their tops.
class Heap {
constructor(compare) {
this.data = [];
this.compare = compare; // negative means a belongs above b
}
size() {
return this.data.length;
}
peek() {
return this.data[0];
}
push(val) {
this.data.push(val);
let i = this.data.length - 1;
while (i > 0) {
const parent = (i - 1) >> 1;
if (this.compare(this.data[i], this.data[parent]) < 0) {
[this.data[i], this.data[parent]] = [this.data[parent], this.data[i]];
i = parent;
} else break;
}
}
pop() {
const top = this.data[0];
const last = this.data.pop();
if (this.data.length > 0) {
this.data[0] = last;
let i = 0;
const n = this.data.length;
while (true) {
let best = i;
const left = 2 * i + 1;
const right = 2 * i + 2;
if (left < n && this.compare(this.data[left], this.data[best]) < 0) best = left;
if (right < n && this.compare(this.data[right], this.data[best]) < 0) best = right;
if (best === i) break;
[this.data[i], this.data[best]] = [this.data[best], this.data[i]];
i = best;
}
}
return top;
}
}
class MedianFinder {
constructor() {
this.lowerHalf = new Heap((a, b) => b - a); // max-heap: smaller half of the numbers
this.upperHalf = new Heap((a, b) => a - b); // min-heap: larger half of the numbers
}
addNum(num) {
if (this.lowerHalf.size() === 0 || num <= this.lowerHalf.peek()) {
this.lowerHalf.push(num);
} else {
this.upperHalf.push(num);
}
// Rebalance so lowerHalf has either the same count as upperHalf, or exactly one more.
if (this.lowerHalf.size() > this.upperHalf.size() + 1) {
this.upperHalf.push(this.lowerHalf.pop());
} else if (this.upperHalf.size() > this.lowerHalf.size()) {
this.lowerHalf.push(this.upperHalf.pop());
}
}
findMedian() {
if (this.lowerHalf.size() === this.upperHalf.size()) {
return (this.lowerHalf.peek() + this.upperHalf.peek()) / 2;
}
return this.lowerHalf.peek();
}
}Time: O(log n) per addNum; O(1) per findMedian · Space: O(n) total across both heaps