0
0
DSA C++programming~10 mins

Median of Data Stream Using Two Heaps in DSA C++ - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to insert a number into the max heap.

DSA C++
maxHeap.[1](num);
Drag options to blanks, or click blank then click option'
Aempty
Bpop
Ctop
Dpush
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop instead of push to add elements.
Trying to access top instead of inserting.
2fill in blank
medium

Complete the code to balance the sizes of the two heaps after insertion.

DSA C++
if (maxHeap.size() > minHeap.size() + [1]) {
    minHeap.push(maxHeap.top());
    maxHeap.pop();
}
Drag options to blanks, or click blank then click option'
A0
B2
C1
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or 2 instead of 1 for size difference.
Not balancing heaps after insertion.
3fill in blank
hard

Fix the error in the code to get the median when heaps are balanced.

DSA C++
return (maxHeap.top() + [1].top()) / 2.0;
Drag options to blanks, or click blank then click option'
AminHeap
BmaxHeap
Cheap
DmedianHeap
Attempts:
3 left
💡 Hint
Common Mistakes
Using maxHeap.top() twice.
Using undefined heap names.
4fill in blank
hard

Fill both blanks to insert a number into the correct heap based on its value.

DSA C++
if (maxHeap.empty() || num [1] maxHeap.top()) {
    maxHeap.[2](num);
} else {
    minHeap.push(num);
}
Drag options to blanks, or click blank then click option'
A<
B>
Cpush
Dpop
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < for comparison.
Using pop instead of push to insert.
5fill in blank
hard

Fill all three blanks to return the median correctly based on heap sizes.

DSA C++
if (maxHeap.size() > minHeap.size()) {
    return maxHeap.[1]();
} else if (minHeap.size() > maxHeap.size()) {
    return minHeap.[2]();
} else {
    return (maxHeap.top() + minHeap.top()) / [3];
}
Drag options to blanks, or click blank then click option'
Atop
C2.0
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using size() instead of top() to get element.
Dividing by 2 instead of 2.0 causing integer division.