Challenge - 5 Problems
Median Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Median After Adding Numbers
What is the median after adding the numbers 5, 15, 1, 3 in order using two heaps?
DSA Javascript
class MedianFinder { constructor() { this.small = [] // max heap this.large = [] // min heap } addNum(num) { if (this.small.length === 0 || num <= -this.small[0]) { this.small.push(-num) this.small.sort((a, b) => a - b) } else { this.large.push(num) this.large.sort((a, b) => a - b) } if (this.small.length > this.large.length + 1) { this.large.push(-this.small.shift()) this.large.sort((a, b) => a - b) } else if (this.large.length > this.small.length) { this.small.push(-this.large.shift()) this.small.sort((a, b) => a - b) } } findMedian() { if (this.small.length > this.large.length) { return -this.small[0] } else { return (-this.small[0] + this.large[0]) / 2 } } } const mf = new MedianFinder() mf.addNum(5) mf.addNum(15) mf.addNum(1) mf.addNum(3) const median = mf.findMedian() console.log(median)
Attempts:
2 left
💡 Hint
Think about how the two heaps balance the numbers and how median is calculated from their tops.
✗ Incorrect
After adding 5 and 15, median is (5+15)/2 = 10. After adding 1, median is 5. After adding 3, numbers are [1,3,5,15], median is (3+5)/2 = 4.
🧠 Conceptual
intermediate1:30remaining
Why Use Two Heaps for Median of Data Stream?
Why do we use 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 stream into lower half (max heap) and upper half (min heap). This allows quick access to middle values for median calculation.
🔧 Debug
advanced2:00remaining
Identify the Bug in MedianFinder Implementation
What error will this code produce when adding numbers and finding median?
DSA Javascript
class MedianFinder { constructor() { this.small = [] // max heap this.large = [] // min heap } addNum(num) { if (this.small.length === 0 || num <= -this.small[0]) { this.small.push(-num) this.small.sort((a, b) => a - b) } else { this.large.push(num) this.large.sort((a, b) => a - b) } if (this.small.length > this.large.length + 1) { this.large.push(-this.small.shift()) this.large.sort((a, b) => a - b) } else if (this.large.length > this.small.length) { this.small.push(-this.large.shift()) this.small.sort((a, b) => a - b) } } findMedian() { if (this.small.length > this.large.length) { return -this.small[0] } else { return (-this.small[0] + this.large[0]) / 2 } } } const mf = new MedianFinder() mf.addNum(1) mf.addNum(2) mf.addNum(3) console.log(mf.findMedian())
Attempts:
2 left
💡 Hint
Think about how shift() affects the heap property after sorting.
✗ Incorrect
The code uses arrays and sorts them to simulate heaps but uses shift() which removes the first element, breaking the heap property. This causes incorrect behavior or errors.
📝 Syntax
advanced2:00remaining
Which Option Fixes the Heap Balancing Logic?
Given the buggy code below, which option correctly fixes the balancing logic to maintain heaps properly?
class MedianFinder {
constructor() {
this.small = [] // max heap
this.large = [] // min heap
}
addNum(num) {
if (this.small.length === 0 || num <= -this.small[0]) {
this.small.push(-num)
this.small.sort()
} else {
this.large.push(num)
this.large.sort()
}
if (this.small.length > this.large.length + 1) {
this.large.push(-this.small.shift())
this.large.sort()
} else if (this.large.length > this.small.length) {
this.small.push(-this.large.shift())
this.small.sort()
}
}
findMedian() {
if (this.small.length > this.large.length) {
return -this.small[0]
} else {
return (-this.small[0] + this.large[0]) / 2
}
}
}
Attempts:
2 left
💡 Hint
Think about how heaps work and how to maintain their properties.
✗ Incorrect
Heaps require specific operations like push and pop with heapify. Using shift() and sorting arrays breaks heap properties. Using pop() with a proper heap fixes this.
🚀 Application
expert2:30remaining
Calculate Median After Complex Stream Operations
Given the following sequence of numbers added to the MedianFinder: 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, what is the median after all insertions?
DSA Javascript
class MedianFinder { constructor() { this.small = [] // max heap this.large = [] // min heap } addNum(num) { if (this.small.length === 0 || num <= -this.small[0]) { this.small.push(-num) this.small.sort((a, b) => a - b) } else { this.large.push(num) this.large.sort((a, b) => a - b) } if (this.small.length > this.large.length + 1) { this.large.push(-this.small.shift()) this.large.sort((a, b) => a - b) } else if (this.large.length > this.small.length) { this.small.push(-this.large.shift()) this.small.sort((a, b) => a - b) } } findMedian() { if (this.small.length > this.large.length) { return -this.small[0] } else { return (-this.small[0] + this.large[0]) / 2 } } } const mf = new MedianFinder() [10,20,30,40,50,60,70,80,90,100].forEach(n => mf.addNum(n)) console.log(mf.findMedian())
Attempts:
2 left
💡 Hint
Count the numbers and find the middle two values for even count.
✗ Incorrect
There are 10 numbers, so median is average of 5th and 6th numbers in sorted order: (50 + 60)/2 = 55.