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 tops of the heaps.
Click to reveal answer
beginner
Why do we balance the sizes of the two heaps when finding the median?
Balancing ensures the heaps differ in size by at most one, so the median is either the top of one heap or the average of both tops.
Click to reveal answer
beginner
In the two heaps method, which heap stores the smaller half of the numbers?
The max heap stores the smaller half, so its top is the largest of the smaller numbers.
Click to reveal answer
intermediate
How do you find the median when the total number of elements is odd using two heaps?
The median is the top of the heap that has one extra element (either max heap or 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 is at the top of the heaps.
Click to reveal answer
Which data structure is used to store the larger half of the numbers in the two heaps method?
AMin heap
BMax heap
CQueue
DStack
✗ Incorrect
The min heap stores the larger half so that the smallest of the larger numbers is at the top.
If the max heap has 5 elements and the min heap has 4 elements, how do you find the median?
AAverage of tops of both heaps
BSum of all elements divided by 9
CTop of max heap
DTop of min heap
✗ Incorrect
When heaps differ by one element, the median is the top of the heap with more elements.
What is the first step when adding a new number to the data stream using two heaps?
AAdd to max heap always
BAdd to max heap if smaller than max heap top, else min heap
CAdd to min heap always
DAdd to both heaps
✗ Incorrect
We compare the new number with max heap top to decide where to add.
Why do we sometimes move elements between heaps after insertion?
ATo clear the heaps
BTo sort the entire data stream
CTo remove duplicates
DTo keep heaps balanced in size
✗ Incorrect
Balancing the heaps ensures correct median calculation.
What is the median when both heaps have the same number of elements?
AAverage of tops of both heaps
BTop of max heap
CTop of min heap
DZero
✗ Incorrect
Median is the average of the two middle values from both heaps.
Explain how two heaps are used to maintain the median in a data stream.
Think about splitting numbers into two groups and how to find the middle.
You got /4 concepts.
Describe the steps to add a new number to the data stream and update the median using two heaps.
Focus on insertion and balancing.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using two heaps (a max-heap and a min-heap) to find 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 remove duplicate numbers from the stream
D. To efficiently keep track of the lower half and upper half of the numbers separately
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 D
Quick Check:
Two heaps split data halves = To efficiently keep track of the lower half and upper half of the numbers separately [OK]
Hint: Two heaps split data halves for quick median [OK]
Common Mistakes:
Thinking heaps sort all data fully
Using one heap only
Confusing heaps with arrays
2. Which of the following is the correct way to insert a new number into the two heaps when maintaining the median?
easy
A. Insert into the heap based on comparison with max-heap top
B. Insert into min-heap only
C. Insert into max-heap only
D. Always insert into min-heap first, then balance heaps
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 A
Quick Check:
Compare with max-heap top to choose heap [OK]
Hint: Compare new number with max-heap top to choose heap [OK]
Common Mistakes:
Always inserting into one heap without condition
Not balancing heaps after insertion
Inserting into wrong heap based on min-heap top
3. Consider the following sequence of numbers inserted into the data stream: 5, 15, 1, 3. What is the median after all insertions using two heaps?
Insert 5
Insert 15
Insert 1
Insert 3
Find median
medium
A. 5
B. 3
C. 4
D. 6
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 C
Quick Check:
Median of [1,3,5,15] = 4 [OK]
Hint: Median is average when heaps equal size [OK]
Common Mistakes:
Taking median as max-heap top only
Not balancing heaps after insertions
Calculating median incorrectly as 5 or 3
4. The following code snippet tries to maintain median using two heaps but has a bug. What is the bug?
A. The balancing pops top element before pushing, causing wrong order
B. The condition to insert into maxHeap is incorrect
C. The heaps are not balanced after insertion
D. The function does not return the median
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 A
Quick Check:
Push top before pop to balance heaps correctly [OK]
Hint: Push top before pop when balancing heaps [OK]
Common Mistakes:
Popping before pushing the correct top
Ignoring heap size difference conditions
Not handling empty heaps properly
5. You have a data stream of integers and want to find the median after each insertion. Using two heaps, how would you handle the case when the total number of elements is odd?
hard
A. The median is always the top of the min-heap
B. The median is the top element of the heap with one extra element
C. The median is always the top of the max-heap
D. The median is the average of the tops of both heaps
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 B
Quick Check:
Odd count median = top of larger heap [OK]
Hint: Median is top of bigger heap when odd count [OK]
Common Mistakes:
Averaging tops when count is odd
Assuming max-heap always has extra element
Ignoring heap size difference
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.