0
0
DSA C++programming~5 mins

Median of Data Stream Using Two Heaps in DSA C++ - 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 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?
AMin-heap
BMax-heap
CQueue
DStack
If the max-heap has 5 elements and the min-heap has 3 elements, what should you do?
AMove the top element from min-heap to max-heap
BMove the top element from max-heap to min-heap
CAdd a new element to min-heap
DDo nothing
What is the median when both heaps have the same number of elements?
AAverage of tops of max-heap and min-heap
BTop of min-heap
CTop of max-heap
DSum of all elements
Why is a max-heap used for the smaller half instead of a min-heap?
ATo store negative numbers
BTo sort numbers in ascending order
CBecause min-heap is slower
DTo quickly access the largest number in the smaller half
What is the first step when adding a new number to the data stream using two heaps?
AAdd to max-heap always
BAdd to min-heap always
CAdd to max-heap if number is smaller than or equal to max-heap top
DAdd to min-heap if number is smaller than max-heap top
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.