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 min-heap array after extracting the minimum element once?
DSA C++
std::vector<int> heap = {1, 3, 6, 5, 9, 8}; // Extract min operation (remove root and bubble down) // After extraction, heap becomes: // 1. Replace root with last element // 2. Remove last element // 3. Bubble down the new root // Resulting heap array:
Attempts:
2 left
💡 Hint
Remember to replace the root with the last element and then bubble down to restore heap property.
✗ Incorrect
After removing the root (1), replace it with last element (8). Then bubble down 8: compare with children 3 and 6, swap with smaller child 3. Now 8 is at index 1, compare with children 5 and 9, swap with smaller child 5. Final heap: [3, 5, 6, 8, 9].
❓ Predict Output
intermediate2:00remaining
Output of Extract-Max on a Max-Heap
What is the state of the max-heap array after extracting the maximum element once?
DSA C++
std::vector<int> heap = {20, 18, 15, 13, 10, 8, 9}; // Extract max operation (remove root and bubble down) // After extraction, heap becomes: // 1. Replace root with last element // 2. Remove last element // 3. Bubble down the new root // Resulting heap array:
Attempts:
2 left
💡 Hint
After replacing root with last element, bubble down by swapping with the larger child.
✗ Incorrect
Remove root (20), replace with last element (9). Bubble down 9: children are 18 and 15, swap with larger child 18. Now at index 1, children 13 and 10, swap with larger child 13. Final heap: [18, 13, 15, 9, 10, 8].
🧠 Conceptual
advanced1:30remaining
Why Bubble Down is Necessary After Extracting Root in a Heap
Why do we need to perform the bubble down operation after extracting the root element from a heap?
Attempts:
2 left
💡 Hint
Think about what happens when the last element replaces the root.
✗ Incorrect
After removing the root, the last element is placed at the root. This element may be larger (in min-heap) or smaller (in max-heap) than its children, violating the heap property. Bubble down moves it down to restore the heap order.
🔧 Debug
advanced2:00remaining
Identify the Bug in Bubble Down Implementation
What is the bug in this bubble down code snippet for a min-heap?
DSA C++
void bubbleDown(std::vector<int>& heap, int index) { int left = 2 * index + 1; int right = 2 * index + 2; int smallest = index; if (left < heap.size() && heap[left] < heap[smallest]) { smallest = left; } if (right < heap.size() && heap[right] < heap[smallest]) { smallest = right; } if (smallest != index) { std::swap(heap[index], heap[smallest]); bubbleDown(heap, smallest); } }
Attempts:
2 left
💡 Hint
Check the conditions and recursive call carefully.
✗ Incorrect
The code correctly finds the smallest child and swaps if needed, then recursively bubbles down. It checks bounds before accessing children. The recursion is valid for typical heap sizes.
🚀 Application
expert1:00remaining
Number of Items After Multiple Extract-Min Operations
Given a min-heap with 10 elements, how many elements remain after performing 3 extract-min operations?
Attempts:
1 left
💡 Hint
Each extract-min removes exactly one element from the heap.
✗ Incorrect
Each extract-min removes the root element, reducing the heap size by one. After 3 extractions, 10 - 3 = 7 elements remain.