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 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 that the heaps represent equal halves of the data or differ by one element, which helps in correctly calculating the median.
Click to reveal answer
beginner
In the two heaps method, what does the max-heap store?
The max-heap stores the smaller half of the numbers, so its top is the largest number in the smaller half.
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 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 is the top element(s) of the heaps.
Click to reveal answer
What data structure is used to store the larger half of the numbers in the two heaps method?
✗ Incorrect
The larger half of the numbers is stored in a min-heap so that the smallest number in the larger half is easily accessible.
If the max-heap has 5 elements and the min-heap has 3 elements, what should you do?
✗ Incorrect
The heaps should be balanced or differ by at most one element. Moving the top element from max-heap to min-heap balances the sizes.
What is the median when both heaps have the same number of elements?
✗ Incorrect
When heaps are equal in size, the median is the average of the two middle values, which are the tops of the two heaps.
Why is a max-heap used for the smaller half instead of a min-heap?
✗ Incorrect
A max-heap allows quick access to the largest number in the smaller half, which helps in balancing and median calculation.
What is the first step when adding a new number to the data stream using two heaps?
✗ Incorrect
The new number is compared with the max-heap top to decide which heap it belongs to, ensuring correct partitioning.
Explain how two heaps are used to maintain the median of a data stream.
Think about dividing numbers into two parts and how to find middle values quickly.
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.