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 is either the top of one heap or the average of the tops of both 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 can be found easily from the top elements of the 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 if the total number of elements is odd using two heaps?
The median is the top element 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 involves looking at the top elements.
Click to reveal answer
Which heap stores the larger half of the numbers in the two heaps approach?
AMax heap
BBoth heaps store the same numbers
CNeither heap stores the larger half
DMin heap
✗ 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?
ARemove the top from max heap and add it to min heap
BRemove the top from min heap and add it to max heap
CDo nothing
DRemove the smallest element from both heaps
✗ Incorrect
To balance, move the largest element from max heap to min heap.
If the total number of elements is even, how is the median calculated?
AAverage of tops of max heap and min heap
BTop of min heap
CTop of max heap
DSum of all elements divided by total count
✗ Incorrect
Median is the average of the largest of smaller half and smallest of larger half.
What is the main advantage of using two heaps for median of data stream?
AConstant time insertion
BConstant time median retrieval
CNo need to sort all elements every time
DUses less memory
✗ Incorrect
Two heaps avoid sorting the entire data stream on each insertion.
Which operation is more expensive when using two heaps: insertion or finding median?
ANeither operation is expensive
BInsertion
CBoth are equally expensive
DFinding median
✗ Incorrect
Insertion involves heap operations O(log n), while median retrieval is O(1).
Explain how two heaps are used to maintain the median in a data stream.
Think about splitting numbers into two groups and how to keep track of the middle.
You got /4 concepts.
Describe the steps to add a new number to the two heaps and update the median.
Consider where the new number fits 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 the Median of Data Stream problem?
easy
A. To sort all numbers in ascending order
B. To store all numbers in a single data structure
C. To efficiently keep track of the lower half and upper half of the numbers separately
D. To remove duplicates from 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 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 = efficient median [OK]
Hint: Two heaps split data halves for quick median [OK]
Common Mistakes:
Thinking heaps sort all numbers fully
Using one heap only
Confusing heaps with arrays
2. Which TypeScript syntax correctly declares a max heap using a priority queue for numbers?
easy
A. const maxHeap = new PriorityQueue<number>((a, b) => b - a);
B. const maxHeap = new PriorityQueue<number>((a, b) => a - b);
C. const maxHeap = new PriorityQueue<number>();
D. const maxHeap = new PriorityQueue<number>((a, b) => a + b);
Solution
Step 1: Understand max heap comparator
Max heap comparator returns positive if first is less than second, so b - a creates max heap.
Step 2: Check each option
const maxHeap = new PriorityQueue<number>((a, b) => b - a); uses (b - a) which is correct for max heap; others do not create max heap order.
Final Answer:
const maxHeap = new PriorityQueue<number>((a, b) => b - a); -> Option A
Quick Check:
Comparator b - a = max heap [OK]
Hint: Max heap comparator subtracts first from second [OK]
Common Mistakes:
Using a - b for max heap (creates min heap)
Not providing comparator
Using addition in comparator
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?
A. It compares num with minHeap.peek() instead of maxHeap.peek()
B. It balances heaps incorrectly by moving elements the wrong way
C. It uses enqueue instead of push for heaps
D. It does not handle empty maxHeap before peek() call
Solution
Step 1: Analyze peek() usage on maxHeap
If maxHeap is empty, calling peek() causes error because no element exists yet.
Step 2: Identify missing empty check
Code should check if maxHeap is empty before peek() or handle initial insertion separately.
Final Answer:
It does not handle empty maxHeap before peek() call -> Option D
Quick Check:
Empty heap peek() causes runtime error [OK]
Hint: Check heap empty before peek() to avoid errors [OK]
Common Mistakes:
Assuming heaps always have elements
Mixing enqueue and push methods
Balancing heaps incorrectly
5. You want to extend the Median of Data Stream solution to support removing numbers from the stream efficiently. Which data structure modification is best to support both insertion and removal while maintaining median calculation?
hard
A. Use two heaps with a hash map to mark removed elements lazily
B. Use two balanced binary search trees (like AVL or Red-Black trees) instead of heaps
C. Use a single sorted array and binary search for insertion and removal
D. Use two queues to store lower and upper halves
Solution
Step 1: Understand removal challenge in heaps
Heaps do not support efficient arbitrary removal, so direct removal is costly.
Step 2: Explain lazy removal with hash map
Mark elements as removed in a hash map and skip them during heap operations to simulate removal efficiently.
Step 3: Compare other options
Balanced trees support removal but are more complex; arrays are inefficient; queues do not maintain order.
Final Answer:
Use two heaps with a hash map to mark removed elements lazily -> Option A
Quick Check:
Lazy removal with hash map + heaps = efficient median update [OK]
Hint: Lazy removal with hash map keeps heaps efficient [OK]
Common Mistakes:
Trying direct removal from heaps
Using arrays causing slow operations
Ignoring complexity of balanced trees
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.