0
0
DSA Goprogramming~5 mins

Median of Data Stream Using Two Heaps in DSA Go - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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 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
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
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
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
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
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.