Recall & Review
beginner
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
beginner
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 the 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) since it involves only the top elements of the heaps.
Click to reveal answer
Which heap stores the larger half of the numbers in the two heaps approach?
✗ 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 more than one extra element compared to the min heap?
✗ 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 is the median found?
✗ 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 calculation in a data stream?
✗ Incorrect
Two heaps allow efficient insertion and quick median retrieval without sorting the entire data.
Which operation is NOT needed when adding a new number to the two heaps?
✗ Incorrect
Sorting all elements is not needed; heaps maintain order efficiently.
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 and update the median using two heaps.
Focus on where to put the new number and how to keep heaps balanced.
You got /4 concepts.