What if you could instantly know the middle value of a never-ending list without sorting it every time?
Why Median of Data Stream Using Two Heaps in DSA C?
Imagine you are tracking the middle value of a growing list of numbers, like recording daily temperatures. You write down each number and then try to find the middle one by sorting all numbers every time a new one arrives.
This manual method is slow and tiring because sorting the entire list after each new number takes a lot of time, especially when the list grows long. It also makes it easy to make mistakes when handling many numbers.
Using two heaps (special trees that keep numbers in order), one for the smaller half and one for the larger half, lets you quickly find the middle number without sorting everything again. This keeps the process fast and simple.
int numbers[1000]; int count = 0; // Insert number numbers[count++] = new_number; // Sort numbers qsort(numbers, count, sizeof(int), compare); // Find median int median = numbers[count / 2];
maxHeapInsert(smallerHalf, new_number);
if (sizeDifferenceTooBig) {
minHeapInsert(largerHalf, maxHeapExtract(smallerHalf));
}
median = getMedianFromHeaps(smallerHalf, largerHalf);This method enables real-time tracking of the middle value in a stream of numbers efficiently, even as the data keeps growing.
Streaming apps use this to show the median rating of a movie as users keep submitting their scores, updating instantly without delays.
Manual sorting after each insertion is slow and error-prone.
Two heaps keep smaller and larger halves separately for quick median access.
This approach handles continuous data efficiently in real time.
Practice
Solution
Step 1: Understand the role of two heaps
The max-heap stores the smaller half of the numbers, and the min-heap stores the larger half.Step 2: Explain how this helps find the median
By balancing these heaps, the median is either the top of one heap or the average of the tops of both heaps.Final Answer:
To efficiently keep track of the lower half and upper half of the numbers separately -> Option DQuick Check:
Two heaps split data halves = To efficiently keep track of the lower half and upper half of the numbers separately [OK]
- Thinking heaps sort all data fully
- Using one heap only
- Confusing heaps with arrays
Solution
Step 1: Decide where to insert the new number
If the new number is less than or equal to the max-heap top, insert into max-heap; otherwise, insert into min-heap.Step 2: Balance the heaps if needed
After insertion, balance the heaps so their sizes differ by at most one.Final Answer:
Insert into the heap based on comparison with max-heap top -> Option AQuick Check:
Compare with max-heap top to choose heap [OK]
- Always inserting into one heap without condition
- Not balancing heaps after insertion
- Inserting into wrong heap based on min-heap top
Insert 5 Insert 15 Insert 1 Insert 3 Find median
Solution
Step 1: Insert numbers and balance heaps
Insert 5 into max-heap ([5]); insert 15 into min-heap ([15]); insert 1 (<=5) into max-heap ([5,1] top=5); insert 3 (<=5) into max-heap ([5,3,1] top=5), sizes 3 vs 1, balance: min.push(5) ([15,5] top=5), max.pop() ([3,1] top=3).Step 2: Calculate median from heaps
Heaps sizes equal (2 and 2), median = average of max-heap top (3) and min-heap top (5) = (3+5)/2 = 4.Final Answer:
4 -> Option CQuick Check:
Median of [1,3,5,15] = 4 [OK]
- Taking median as max-heap top only
- Not balancing heaps after insertions
- Calculating median incorrectly as 5 or 3
void addNumber(int num, priority_queue<int>& maxHeap, priority_queue& minHeap) {
if (maxHeap.empty() || num <= maxHeap.top()) {
maxHeap.push(num);
} else {
minHeap.push(num);
}
if (maxHeap.size() > minHeap.size() + 1) {
minHeap.push(maxHeap.top());
maxHeap.pop();
} else if (minHeap.size() > maxHeap.size() + 1) {
maxHeap.push(minHeap.top());
minHeap.pop();
}
}Solution
Step 1: Analyze balancing logic
When balancing, the code pops the top element of one heap before pushing the new top to the other, which moves the wrong element.Step 2: Explain correct balancing
Correct approach is to push heap.top() first, then pop() to transfer the correct top element.Final Answer:
The balancing pops top element before pushing, causing wrong order -> Option AQuick Check:
Push top before pop to balance heaps correctly [OK]
- Popping before pushing the correct top
- Ignoring heap size difference conditions
- Not handling empty heaps properly
Solution
Step 1: Understand heap size difference
When total elements are odd, one heap will have one more element than the other.Step 2: Determine median from heaps
The median is the top element of the heap that has the extra element, because it holds the middle value.Final Answer:
The median is the top element of the heap with one extra element -> Option BQuick Check:
Odd count median = top of larger heap [OK]
- Averaging tops when count is odd
- Assuming max-heap always has extra element
- Ignoring heap size difference
Ready to prove it?
Pass the test and this pattern moves unknown
