Challenge - 5 Problems
Median Mastery Badge
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 Go
package main import ( "container/heap" "fmt" ) // IntHeap is a min-heap of ints. type IntHeap []int func (h IntHeap) Len() int { return len(h) } func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] } func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } func (h *IntHeap) Push(x interface{}) { *h = append(*h, x.(int)) heap.Fix(h, h.Len()-1) } func (h *IntHeap) Pop() interface{} { n := h.Len() x := (*h)[0] (*h)[0] = (*h)[n-1] *h = (*h)[:n-1] heap.Fix(h, 0) return x } // MaxHeap is a max-heap of ints (invert Less). type MaxHeap []int func (h MaxHeap) Len() int { return len(h) } func (h MaxHeap) Less(i, j int) bool { return h[i] > h[j] } func (h MaxHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } func (h *MaxHeap) Push(x interface{}) { *h = append(*h, x.(int)) heap.Fix(h, h.Len()-1) } func (h *MaxHeap) Pop() interface{} { n := h.Len() x := (*h)[0] (*h)[0] = (*h)[n-1] *h = (*h)[:n-1] heap.Fix(h, 0) return x } func main() { low := &MaxHeap{} high := &IntHeap{} heap.Init(low) heap.Init(high) nums := []int{5, 15, 1, 3} for _, num := range nums { if low.Len() == 0 || num <= (*low)[0] { heap.Push(low, num) } else { heap.Push(high, num) } // Balance heaps if low.Len() > high.Len()+1 { val := heap.Pop(low).(int) heap.Push(high, val) } else if high.Len() > low.Len() { val := heap.Pop(high).(int) heap.Push(low, val) } // Calculate median var median float64 if low.Len() == high.Len() { median = float64((*low)[0]+(*high)[0]) / 2.0 } else { median = float64((*low)[0]) } fmt.Printf("Median after adding %d: %.1f\n", num, median) } }
Attempts:
2 left
💡 Hint
Balance the two heaps so their sizes differ by at most one. Median is top of max-heap or average of tops.
✗ Incorrect
We add numbers one by one, pushing smaller half into max-heap and larger half into min-heap. Balancing ensures median is either top of max-heap or average of tops.
🧠 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 splits data into two halves.
✗ Incorrect
Two heaps split the data into smaller and larger halves. Max-heap stores smaller half so its top is the largest of smaller numbers. Min-heap stores larger half so its top is the smallest of larger numbers. Median is either top of max-heap or average of tops.
🔧 Debug
advanced1:30remaining
Identify the Bug in Median Calculation Code
What error will this code produce when calculating median after adding numbers using two heaps?
Code snippet:
if low.Len() == high.Len() {
median = float64(((*low)[0] + (*high)[0]) / 2)
} else {
median = float64((*low)[0])
}
Note: low is max-heap, high is min-heap.
Attempts:
2 left
💡 Hint
Check how division works between integers and floats in Go.
✗ Incorrect
In Go, integer division truncates before converting to float64. Dividing by 2 (int) causes truncation. Dividing by 2.0 (float64) ensures correct float division.
❓ Predict Output
advanced2:00remaining
Output After Balancing Heaps
Given the sequence of numbers added: 10, 20, 30, 40, 50, what is the content of the max-heap (low) and min-heap (high) after all insertions and balancing?
DSA Go
package main import ( "container/heap" "fmt" ) type IntHeap []int func (h IntHeap) Len() int { return len(h) } func (h IntHeap) Less(i, j int) bool { return h[i] < h[j] } func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } func (h *IntHeap) Push(x interface{}) { *h = append(*h, x.(int)) heap.Fix(h, h.Len()-1) } func (h *IntHeap) Pop() interface{} { n := h.Len() x := (*h)[0] (*h)[0] = (*h)[n-1] *h = (*h)[:n-1] heap.Fix(h, 0) return x } type MaxHeap []int func (h MaxHeap) Len() int { return len(h) } func (h MaxHeap) Less(i, j int) bool { return h[i] > h[j] } func (h MaxHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } func (h *MaxHeap) Push(x interface{}) { *h = append(*h, x.(int)) heap.Fix(h, h.Len()-1) } func (h *MaxHeap) Pop() interface{} { n := h.Len() x := (*h)[0] (*h)[0] = (*h)[n-1] *h = (*h)[:n-1] heap.Fix(h, 0) return x } func main() { low := &MaxHeap{} high := &IntHeap{} heap.Init(low) heap.Init(high) nums := []int{10, 20, 30, 40, 50} for _, num := range nums { if low.Len() == 0 || num <= (*low)[0] { heap.Push(low, num) } else { heap.Push(high, num) } if low.Len() > high.Len()+1 { val := heap.Pop(low).(int) heap.Push(high, val) } else if high.Len() > low.Len() { val := heap.Pop(high).(int) heap.Push(low, val) } } fmt.Printf("Max-heap (low): %v\n", *low) fmt.Printf("Min-heap (high): %v\n", *high) }
Attempts:
2 left
💡 Hint
Remember max-heap stores smaller half, min-heap stores larger half, and heaps are balanced in size.
✗ Incorrect
After inserting all numbers and balancing, max-heap contains the smaller half [30, 10, 20] (max-heap order), min-heap contains larger half [40, 50].
🚀 Application
expert2:30remaining
Calculate Median After Complex Stream
Given the stream of numbers: 2, 8, 3, 5, 10, 9, 4, 7, 6, what is the median after all numbers are added using two heaps?
Attempts:
2 left
💡 Hint
Balance heaps after each insertion and find median from tops.
✗ Incorrect
After adding all numbers and balancing heaps, the median is the middle value 6.0 because the total count is odd (9).