0
0
DSA C++programming~20 mins

Median of Data Stream Using Two Heaps in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Median Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Median after inserting numbers
What is the median after inserting the following numbers in order: 5, 15, 1, 3 using the two heaps approach?
DSA C++
class MedianFinder {
private:
    std::priority_queue<int> maxHeap; // lower half
    std::priority_queue<int, std::vector<int>, std::greater<int>> minHeap; // upper half

public:
    void addNum(int num) {
        if (maxHeap.empty() || num <= maxHeap.top()) {
            maxHeap.push(num);
        } else {
            minHeap.push(num);
        }

        if (maxHeap.size() > minHeap.size() + 1) {
            minHeap.push(maxHeap.top());
            maxHeap.pop();
        } else if (minHeap.size() > maxHeap.size()) {
            maxHeap.push(minHeap.top());
            minHeap.pop();
        }
    }

    double findMedian() {
        if (maxHeap.size() == minHeap.size()) {
            return (maxHeap.top() + minHeap.top()) / 2.0;
        } else {
            return maxHeap.top();
        }
    }
};

MedianFinder mf;
mf.addNum(5);
mf.addNum(15);
mf.addNum(1);
mf.addNum(3);
return mf.findMedian();
A3.0
B4.0
C5.0
D6.0
Attempts:
2 left
💡 Hint
Remember to balance the two heaps after each insertion and then find the median based on their sizes.
🧠 Conceptual
intermediate
1:30remaining
Why use two heaps for median?
Why do we use two heaps (a max heap and a min heap) to find the median in a data stream?
ATo store all numbers in max heap and min heap separately for backup
BTo sort all numbers in ascending order using two heaps
CTo keep the smaller half of numbers in max heap and larger half in min heap for quick median calculation
DTo use heaps for faster insertion only, median is calculated by sorting later
Attempts:
2 left
💡 Hint
Think about how median relates to the middle values of sorted data.
🔧 Debug
advanced
2:00remaining
Identify the bug in heap balancing
What is the bug in the following code snippet for balancing heaps after insertion?
DSA C++
if (maxHeap.size() > minHeap.size()) {
    minHeap.push(maxHeap.top());
    maxHeap.pop();
} else if (minHeap.size() > maxHeap.size() + 1) {
    maxHeap.push(minHeap.top());
    minHeap.pop();
}
AThe conditions for balancing are reversed; maxHeap should allow one extra element, not minHeap
BThe pop operations are missing after pushing elements
CThe code does not check for empty heaps before accessing top()
DThe code balances heaps correctly, no bug
Attempts:
2 left
💡 Hint
Check which heap should have the extra element when sizes differ by one.
Predict Output
advanced
2:00remaining
Median after multiple insertions
What is the median after inserting these numbers in order: 2, 4, 7, 1, 5, 3 using the two heaps approach?
DSA C++
MedianFinder mf;
mf.addNum(2);
mf.addNum(4);
mf.addNum(7);
mf.addNum(1);
mf.addNum(5);
mf.addNum(3);
return mf.findMedian();
A3.5
B4.0
C3.0
D4.5
Attempts:
2 left
💡 Hint
Balance heaps after each insertion and find the middle two numbers for even count.
🧠 Conceptual
expert
1:30remaining
Time complexity of median finding with two heaps
What is the time complexity of adding a number and finding the median using the two heaps approach?
AAdding a number: O(log n), Finding median: O(log n)
BAdding a number: O(n), Finding median: O(log n)
CAdding a number: O(1), Finding median: O(n)
DAdding a number: O(log n), Finding median: O(1)
Attempts:
2 left
💡 Hint
Consider heap insertion and top element access times.