What is the main idea behind using two heaps to find the median of a data stream?
Use a max-heap to store the smaller half of numbers and a min-heap to store the larger half. This way, the median can be quickly found from the top elements of the heaps.
Click to reveal answer
beginner
Why do we balance the sizes of the two heaps when finding the median?
Balancing ensures that the heaps differ in size by at most one, so the median is either the top of one heap or the average of the tops of both heaps.
Click to reveal answer
beginner
In the two heaps approach, which heap stores the smaller half of the numbers?
The max-heap stores the smaller half of the numbers, so its top is the largest of the smaller half.
Click to reveal answer
intermediate
How do you find the median when the total number of elements is even using two heaps?
The median is the average of the top elements of the max-heap and min-heap.
Click to reveal answer
intermediate
What is the time complexity of adding a number and finding the median using two heaps?
Adding a number takes O(log n) due to heap insertion, and finding the median is O(1) because it uses the top elements of the heaps.
Click to reveal answer
Which heap stores the larger half of the numbers in the two heaps approach?
AMin-heap
BMax-heap
CBoth heaps store the same numbers
DNone of the above
✗ Incorrect
The min-heap stores the larger half of the numbers, so its top is the smallest of the larger half.
What do you do if the max-heap has two more elements than the min-heap after insertion?
ADo nothing
BRemove the top from min-heap and add it to max-heap
CRemove the top from max-heap and add it to min-heap
DClear both heaps
✗ Incorrect
To balance the heaps, move the top element from the larger max-heap to the min-heap.
If the total number of elements is odd, where does the median come from?
ATop of min-heap
BTop of max-heap
CAverage of tops of both heaps
DBottom of max-heap
✗ Incorrect
When odd, the max-heap has one extra element, so the median is its top.
What is the main advantage of using two heaps for median of data stream?
AUses less memory than arrays
BOnly works for sorted data
CNo need to balance heaps
DFast insertion and fast median retrieval
✗ Incorrect
Two heaps allow O(log n) insertion and O(1) median retrieval.
Which operation is NOT needed when adding a new number to the two heaps?
ASort the entire data stream
BInsert into one heap
CBalance the heaps
DCompare with max-heap top
✗ Incorrect
Sorting the entire data stream is not needed; heaps keep order efficiently.
Explain how two heaps are used to find the median in a data stream.
Think about splitting numbers into two groups and how to keep track of middle values.
You got /4 concepts.
Describe the steps to add a new number to the two heaps and maintain the median.
Focus on where to put the new number and how to keep heaps balanced.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using two heaps (a max-heap and a min-heap) in finding the median of a data stream?
easy
A. To store all numbers in a single data structure
B. To sort all numbers in ascending order
C. To efficiently keep track of the lower half and upper half of the numbers separately
D. To find the maximum number in the data stream
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 C
Quick Check:
Two heaps split data halves = A [OK]
Hint: Two heaps split data halves for median [OK]
Common Mistakes:
Thinking heaps sort all numbers fully
Using one heap only
Confusing max-heap and min-heap roles
2. Which of the following Go code snippets correctly initializes a max-heap and a min-heap for integers using container/heap package?
easy
A. maxHeap := &IntMaxHeap{}; minHeap := &IntMinHeap{}
B. maxHeap := heap.New(IntMaxHeap{}); minHeap := heap.New(IntMinHeap{})
C. maxHeap := &IntMinHeap{}; minHeap := &IntMaxHeap{}
D. maxHeap := make([]int, 0); minHeap := make([]int, 0)
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 A
Quick Check:
Pointer to heap struct = A [OK]
Hint: Use pointers to heap structs for container/heap [OK]
Common Mistakes:
Using heap.New which does not exist
Swapping max-heap and min-heap types
Initializing as slices without heap.Interface
3. Given the following sequence of numbers inserted into the data stream: 5, 15, 1, 3, what is the median after all insertions using two heaps?
Heaps sizes equal (2 each). Median = average of maxHeap top (3) and minHeap top (5) = (3+5)/2 = 4.
Final Answer:
4 -> Option D
Quick Check:
Median = (3+5)/2 = 4 [OK]
Hint: Median is average of two heap tops if sizes equal [OK]
Common Mistakes:
Taking median as maxHeap top only
Not balancing heaps after insertion
Calculating median as sum instead of average
4. In the following Go code snippet for median calculation, what is the bug?
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))
}
}
medium
A. Heaps are not balanced after insertion
B. Comparison operator should be < instead of <=
C. heap.Pop returns interface{}, needs type assertion before Push
D. maxHeap and minHeap are swapped in conditions
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 A
Quick Check:
minHeap balance: > max.Len() [OK]
Hint: minHeap balance when len(min) > len(max) [OK]
Common Mistakes:
Using +1 symmetrically for both balance conditions
Wrong size difference in minHeap check
Not balancing after every insertion
5. You want to find the median of a large data stream where numbers can be negative, zero, or positive. Which approach using two heaps ensures correct median calculation even with duplicates and negative values?
hard
A. Use a single max-heap and pop elements until median is found
B. Use a max-heap for lower half and min-heap for upper half, balance sizes, and calculate median from tops
C. Sort all numbers in an array and pick middle element(s) each time
D. Use two min-heaps and merge their outputs to find median