Challenge - 5 Problems
Median Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the median after inserting these numbers?
Given the sequence of numbers inserted into the data stream: 5, 15, 1, 3, what is the median after all insertions using the two heaps method?
DSA Python
from heapq import heappush, heappop class MedianFinder: def __init__(self): self.small = [] # max heap (invert values) self.large = [] # min heap def addNum(self, num): heappush(self.small, -num) if self.small and self.large and (-self.small[0]) > self.large[0]: val = -heappop(self.small) heappush(self.large, val) if len(self.small) > len(self.large) + 1: val = -heappop(self.small) heappush(self.large, val) elif len(self.large) > len(self.small) + 1: val = heappop(self.large) heappush(self.small, -val) def findMedian(self): if len(self.small) > len(self.large): return float(-self.small[0]) elif len(self.large) > len(self.small): return float(self.large[0]) else: return (-self.small[0] + self.large[0]) / 2 finder = MedianFinder() numbers = [5, 15, 1, 3] for num in numbers: finder.addNum(num) median = finder.findMedian() print(median)
Attempts:
2 left
💡 Hint
Remember to balance the two heaps after each insertion and then calculate the median based on their sizes.
✗ Incorrect
After inserting 5, 15, 1, 3, the two heaps hold values such that the median is the average of the two middle numbers: (3 + 5) / 2 = 4.0.
🧠 Conceptual
intermediateWhy use two heaps to find the median in a data stream?
What is the main reason for using two heaps (a max heap and a min heap) to find the median in a data stream?
Attempts:
2 left
💡 Hint
Think about how median relates to the middle values in a sorted list.
✗ Incorrect
Two heaps split the data into smaller and larger halves, allowing quick access to the middle values needed for median calculation.
🔧 Debug
advancedIdentify the error in this median finder implementation
What error will this code produce when adding numbers to the MedianFinder?
DSA Python
from heapq import heappush, heappop class MedianFinder: def __init__(self): self.small = [] # max heap (invert values) self.large = [] # min heap def addNum(self, num): if not self.small or num <= -self.small[0]: heappush(self.small, -num) else: heappush(self.large, num) if len(self.small) > len(self.large) + 1: heappush(self.large, -heappop(self.small)) elif len(self.large) > len(self.small) + 1: heappush(self.small, -heappop(self.large)) def findMedian(self): if len(self.small) > len(self.large): return float(-self.small[0]) elif len(self.large) > len(self.small): return float(self.large[0]) else: return (-self.small[0] + self.large[0]) / 2 finder = MedianFinder() numbers = [10, 20, 30] for num in numbers: finder.addNum(num) print(finder.findMedian())
Attempts:
2 left
💡 Hint
Check how values are pushed and popped between heaps.
✗ Incorrect
The code pushes negative values into the min heap, which breaks the heap property and causes incorrect behavior or errors.
❓ Predict Output
advancedWhat is the median after these insertions?
Using the two heaps method, what is the median after inserting the numbers 2, 4, 7, 1, 5, 3?
DSA Python
from heapq import heappush, heappop class MedianFinder: def __init__(self): self.small = [] # max heap (invert values) self.large = [] # min heap def addNum(self, num): heappush(self.small, -num) if self.small and self.large and (-self.small[0]) > self.large[0]: val = -heappop(self.small) heappush(self.large, val) if len(self.small) > len(self.large) + 1: val = -heappop(self.small) heappush(self.large, val) elif len(self.large) > len(self.small) + 1: val = heappop(self.large) heappush(self.small, -val) def findMedian(self): if len(self.small) > len(self.large): return float(-self.small[0]) elif len(self.large) > len(self.small): return float(self.large[0]) else: return (-self.small[0] + self.large[0]) / 2 finder = MedianFinder() numbers = [2, 4, 7, 1, 5, 3] for num in numbers: finder.addNum(num) median = finder.findMedian() print(median)
Attempts:
2 left
💡 Hint
Balance the heaps after each insertion and then find the middle value(s).
✗ Incorrect
After all insertions, the two heaps balance so the median is the average of 3 and 4, which is 3.5.
🧠 Conceptual
expertWhat is the time complexity of finding median using two heaps?
What is the time complexity of adding a number and finding the median in a data stream using the two heaps approach?
Attempts:
2 left
💡 Hint
Consider heap insertion and peek operations.
✗ Incorrect
Adding a number requires heap insertion which is O(log n), but finding median is just accessing the top elements which is O(1).
