Challenge - 5 Problems
Heap Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Extract-Min on a Min-Heap
What is the state of the heap array after extracting the minimum element once from this min-heap?
Initial heap array (index 0 is root): [2, 5, 3, 8, 10, 15]
Initial heap array (index 0 is root): [2, 5, 3, 8, 10, 15]
DSA Go
heap := []int{2, 5, 3, 8, 10, 15} // Extract min operation min := heap[0] heap[0] = heap[len(heap)-1] heap = heap[:len(heap)-1] // Bubble down index := 0 for { left := 2*index + 1 right := 2*index + 2 smallest := index if left < len(heap) && heap[left] < heap[smallest] { smallest = left } if right < len(heap) && heap[right] < heap[smallest] { smallest = right } if smallest == index { break } heap[index], heap[smallest] = heap[smallest], heap[index] index = smallest } fmt.Println(heap)
Attempts:
2 left
💡 Hint
Remember to replace the root with the last element and then bubble down by swapping with the smaller child.
✗ Incorrect
After removing 2, the last element 15 moves to root. Then bubble down swaps 15 with 3 (smaller child), and stops because the new position (index 2) has no children (left child at index 5 >= length 5). Final heap is [3, 5, 15, 8, 10].
❓ Predict Output
intermediate2:00remaining
Output of Extract-Max on a Max-Heap
Given this max-heap array:
[20, 18, 15, 10, 12, 9, 8]
What is the heap array after extracting the maximum element once?
[20, 18, 15, 10, 12, 9, 8]
What is the heap array after extracting the maximum element once?
DSA Go
heap := []int{20, 18, 15, 10, 12, 9, 8} max := heap[0] heap[0] = heap[len(heap)-1] heap = heap[:len(heap)-1] index := 0 for { left := 2*index + 1 right := 2*index + 2 largest := index if left < len(heap) && heap[left] > heap[largest] { largest = left } if right < len(heap) && heap[right] > heap[largest] { largest = right } if largest == index { break } heap[index], heap[largest] = heap[largest], heap[index] index = largest } fmt.Println(heap)
Attempts:
2 left
💡 Hint
After replacing root with last element, bubble down by swapping with the larger child.
✗ Incorrect
After removing 20, 8 moves to root. It swaps with 18 (larger child), then at index 1 (now 8) swaps with 12 (larger child there), then stops. Final heap is [18, 12, 15, 10, 8, 9].
🔧 Debug
advanced2:00remaining
Identify the Bug in Bubble Down for Min-Heap
This Go code tries to bubble down after extract-min on a min-heap. What error will it cause when run?
DSA Go
heap := []int{1, 3, 6, 5, 9, 8} heap[0] = heap[len(heap)-1] heap = heap[:len(heap)-1] index := 0 for { left := 2*index + 1 right := 2*index + 2 smallest := index if left < len(heap) && heap[left] > heap[smallest] { smallest = left } if right < len(heap) && heap[right] < heap[smallest] { smallest = right } if smallest == index { break } heap[index], heap[smallest] = heap[smallest], heap[index] index = smallest } fmt.Println(heap)
Attempts:
2 left
💡 Hint
Check the comparison signs in the if conditions for choosing smallest child.
✗ Incorrect
The first if uses '>' instead of '<' causing wrong child selection. This breaks min-heap property but no runtime error occurs.
🧠 Conceptual
advanced1:30remaining
Why Bubble Down is Needed After Extract-Min or Extract-Max?
After removing the root element from a heap and replacing it with the last element, why do we need to bubble down?
Attempts:
2 left
💡 Hint
Think about what happens to the heap property after replacing the root.
✗ Incorrect
Replacing the root with the last element may break the heap property. Bubble down moves this element down to restore the heap order.
❓ Predict Output
expert3:00remaining
Final Heap After Multiple Extract-Min Operations
Starting with this min-heap array:
[4, 7, 6, 10, 9, 15, 20]
Perform extract-min operation twice in a row. What is the heap array after these two extractions?
[4, 7, 6, 10, 9, 15, 20]
Perform extract-min operation twice in a row. What is the heap array after these two extractions?
DSA Go
heap := []int{4, 7, 6, 10, 9, 15, 20} for i := 0; i < 2; i++ { heap[0] = heap[len(heap)-1] heap = heap[:len(heap)-1] index := 0 for { left := 2*index + 1 right := 2*index + 2 smallest := index if left < len(heap) && heap[left] < heap[smallest] { smallest = left } if right < len(heap) && heap[right] < heap[smallest] { smallest = right } if smallest == index { break } heap[index], heap[smallest] = heap[smallest], heap[index] index = smallest } } fmt.Println(heap)
Attempts:
2 left
💡 Hint
Perform extract-min twice carefully, bubbling down each time.
✗ Incorrect
First extraction removes 4, heap becomes [6,7,15,10,9,20] after bubble down. Second extraction removes 6, heap becomes [7,9,15,10,20].