Challenge - 5 Problems
Heap Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
When to use a Min Heap?
Which scenario is best suited for using a Min Heap?
Attempts:
2 left
💡 Hint
Think about which heap type keeps the smallest element at the top.
✗ Incorrect
A Min Heap always keeps the smallest element at the root, making it ideal for quickly accessing or removing the smallest item in a changing dataset.
🧠 Conceptual
intermediate2:00remaining
When to use a Max Heap?
Which use case is best for a Max Heap?
Attempts:
2 left
💡 Hint
Max Heap keeps the largest element at the root.
✗ Incorrect
A Max Heap keeps the largest element at the root, making it perfect for quickly accessing or removing the largest item in a dataset.
❓ Predict Output
advanced3:00remaining
Output of Min Heap after insertions
What is the printed state of the Min Heap after inserting these elements in order: 7, 3, 5, 1?
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)) } func (h *IntHeap) Pop() interface{} { old := *h n := len(old) x := old[n-1] *h = old[0 : n-1] return x } func main() { h := &IntHeap{} heap.Init(h) heap.Push(h, 7) heap.Push(h, 3) heap.Push(h, 5) heap.Push(h, 1) for h.Len() > 0 { fmt.Print(heap.Pop(h)) if h.Len() > 0 { fmt.Print(" -> ") } } fmt.Println(" -> null") }
Attempts:
2 left
💡 Hint
Min Heap pops elements in ascending order.
✗ Incorrect
After inserting 7, 3, 5, 1 into a Min Heap, popping elements one by one returns them in ascending order: 1, 3, 5, 7.
❓ Predict Output
advanced3:00remaining
Output of Max Heap after insertions
What is the printed state of the Max Heap after inserting these elements in order: 2, 9, 4, 6?
DSA Go
package main import ( "container/heap" "fmt" ) 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)) } func (h *MaxHeap) Pop() interface{} { old := *h n := len(old) x := old[n-1] *h = old[0 : n-1] return x } func main() { h := &MaxHeap{} heap.Init(h) heap.Push(h, 2) heap.Push(h, 9) heap.Push(h, 4) heap.Push(h, 6) for h.Len() > 0 { fmt.Print(heap.Pop(h)) if h.Len() > 0 { fmt.Print(" -> ") } } fmt.Println(" -> null") }
Attempts:
2 left
💡 Hint
Max Heap pops elements in descending order.
✗ Incorrect
After inserting 2, 9, 4, 6 into a Max Heap, popping elements one by one returns them in descending order: 9, 6, 4, 2.
🚀 Application
expert3:00remaining
Choosing Heap Type for Median Maintenance
You want to maintain the median of a stream of numbers efficiently. Which combination of heaps should you use?
Attempts:
2 left
💡 Hint
Think about splitting the data into two halves around the median.
✗ Incorrect
To maintain the median efficiently, use a Max Heap to store the smaller half of numbers and a Min Heap to store the larger half. This allows quick access to the middle values.