What if you could find the middle value instantly, no matter how many numbers you add?
Why Median of Data Stream Using Two Heaps in DSA Go?
Imagine you are tracking the middle value of a growing list of numbers, like recording daily temperatures. You write down every number and then sort the entire list each time you want to find the middle value.
Sorting the whole list every time is slow and tiresome, especially as the list grows longer. It wastes time and can cause mistakes when done manually or with simple code.
Using two heaps (one for smaller half, one for larger half) keeps the numbers balanced and lets you quickly find the middle value without sorting everything again and again.
numbers = [] numbers.append(newNumber) numbers.sort() median = numbers[len(numbers)//2]
smallHeap.Push(newNumber) // Balance heaps median = smallHeap.Top()
This method lets you find the median instantly as new numbers come in, making real-time data analysis smooth and fast.
Streaming apps use this to show live median ratings or prices without delays, even when millions of users add data continuously.
Manual sorting each time is slow and inefficient.
Two heaps keep data balanced for quick median access.
Enables fast, real-time median calculation on growing data.
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 CQuick Check:
Two heaps split data halves = A [OK]
- Thinking heaps sort all numbers fully
- Using one heap only
- Confusing max-heap and min-heap roles
Solution
Step 1: Recognize heap initialization in Go
Heaps are initialized as pointers to custom heap types that implement heap.Interface.Step 2: Identify correct syntax
Using &IntMaxHeap{} and &IntMinHeap{} creates pointers to heap structs ready for heap operations.Final Answer:
maxHeap := &IntMaxHeap{}; minHeap := &IntMinHeap{} -> Option AQuick Check:
Pointer to heap struct = A [OK]
- Using heap.New which does not exist
- Swapping max-heap and min-heap types
- Initializing as slices without heap.Interface
5, 15, 1, 3, what is the median after all insertions using two heaps?Solution
Step 1: Insert numbers and balance heaps
Insert 5 (maxHeap top=5); insert 15 (minHeap top=15); insert 1 ≤5 (maxHeap top=5, contains 5,1); insert 3 ≤5 (maxHeap top=5, contains 5,1,3), balance: len=3 >1+1, pop 5 -> push minHeap top=5 (15,5), maxHeap top=3 (1,3).Step 2: Calculate median from heaps
Heaps sizes equal (2 each). Median = average of maxHeap top (3) and minHeap top (5) = (3+5)/2 = 4.Final Answer:
4 -> Option DQuick Check:
Median = (3+5)/2 = 4 [OK]
- Taking median as maxHeap top only
- Not balancing heaps after insertion
- Calculating median as sum instead of average
func addNum(num int) {
if maxHeap.Len() == 0 || num <= (*maxHeap)[0] {
heap.Push(maxHeap, num)
} else {
heap.Push(minHeap, num)
}
if maxHeap.Len() > minHeap.Len()+1 {
heap.Push(minHeap, heap.Pop(maxHeap))
} else if minHeap.Len() > maxHeap.Len() {
heap.Push(maxHeap, heap.Pop(minHeap))
}
}Solution
Step 1: Analyze balancing logic
The first condition correctly balances when maxHeap is too large (> minHeap.Len()+1). But the second condition checks minHeap.Len() > maxHeap.Len(), which triggers rebalance correctly when minHeap is larger.Step 2: Why heaps are not balanced
Original code used > maxHeap.Len() + 1 which fails to rebalance when minHeap is exactly one larger. Changing to > maxHeap.Len() fixes this.Final Answer:
Heaps are not balanced after insertion -> Option AQuick Check:
minHeap balance: > max.Len() [OK]
- Using +1 symmetrically for both balance conditions
- Wrong size difference in minHeap check
- Not balancing after every insertion
Solution
Step 1: Choose correct heap types for halves
Max-heap stores lower half (including negatives), min-heap stores upper half, handling all values correctly.Step 2: Balance heaps and compute median
Keep heaps balanced in size; median is top of max-heap if odd count, or average of tops if even count.Final Answer:
Use a max-heap for lower half and min-heap for upper half, balance sizes, and calculate median from tops -> Option BQuick Check:
Two heaps balanced handle negatives and duplicates [OK]
- Using two min-heaps which can't split halves properly
- Sorting entire stream each time (inefficient)
- Using single heap only
Ready to prove it?
Pass the test and this pattern moves unknown
