Challenge - 5 Problems
Heap Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output after extracting min from this min-heap?
Given the min-heap array representation, what is the heap array after extracting the minimum element and performing bubble down?
DSA Javascript
const heap = [2, 5, 3, 9, 7]; function extractMin(heap) { const min = heap[0]; heap[0] = heap.pop(); let i = 0; while (true) { let left = 2 * i + 1; let right = 2 * i + 2; let smallest = i; if (left < heap.length && heap[left] < heap[smallest]) smallest = left; if (right < heap.length && heap[right] < heap[smallest]) smallest = right; if (smallest === i) break; [heap[i], heap[smallest]] = [heap[smallest], heap[i]]; i = smallest; } return heap; } console.log(extractMin(heap));
Attempts:
2 left
💡 Hint
Remember after removing the root, replace it with the last element and bubble down to restore heap property.
✗ Incorrect
After removing 2, replace root with 7. Bubble down swaps 7 with 3 (left child). Final heap is [3, 5, 7, 9].
❓ Predict Output
intermediate2:00remaining
What is the max-heap array after extracting max and bubbling down?
Given this max-heap array, what is the array after extracting the maximum element and performing bubble down?
DSA Javascript
const heap = [20, 15, 18, 8, 10, 17]; function extractMax(heap) { const max = heap[0]; heap[0] = heap.pop(); let i = 0; while (true) { let left = 2 * i + 1; let right = 2 * i + 2; let largest = i; if (left < heap.length && heap[left] > heap[largest]) largest = left; if (right < heap.length && heap[right] > heap[largest]) largest = right; if (largest === i) break; [heap[i], heap[largest]] = [heap[largest], heap[i]]; i = largest; } return heap; } console.log(extractMax(heap));
Attempts:
2 left
💡 Hint
After removing the root, replace it with the last element and bubble down to maintain max-heap.
✗ Incorrect
After removing 20, replace root with 17. Bubble down swaps 17 with 18 (right child). Final heap is [18, 15, 17, 8, 10].
🔧 Debug
advanced2:00remaining
Why does this bubble down function fail to restore min-heap?
This bubble down function is supposed to restore min-heap after extraction but fails on some inputs. What is the bug?
DSA Javascript
function bubbleDown(heap, i) {
let left = 2 * i + 1;
let right = 2 * i + 2;
let smallest = i;
if (left < heap.length && heap[left] < heap[smallest]) smallest = left;
if (right < heap.length && heap[right] < heap[smallest]) smallest = right;
if (smallest !== i) {
[heap[i], heap[smallest]] = [heap[smallest], heap[i]];
bubbleDown(heap, smallest);
}
}
const heap = [1, 3, 6, 5, 9, 8];
heap[0] = heap[heap.length - 1];
bubbleDown(heap, 0);
console.log(heap);Attempts:
2 left
💡 Hint
Check how the heap length changes after popping and how it affects index checks.
✗ Incorrect
After popping, heap length decreases but bubbleDown uses old length causing index checks to fail and possible out-of-bounds access.
🧠 Conceptual
advanced1:00remaining
What is the time complexity of bubble down operation in a heap?
Consider a heap with n elements. What is the time complexity of the bubble down operation after extracting the root?
Attempts:
2 left
💡 Hint
Think about the height of a binary heap and how many swaps bubble down can perform.
✗ Incorrect
Bubble down moves down the height of the heap, which is log n for n elements, so time complexity is O(log n).
🚀 Application
expert3:00remaining
After multiple extractMin operations, what is the heap array?
Starting with this min-heap array, perform extractMin twice (removing min and bubbling down each time). What is the resulting heap array?
DSA Javascript
let heap = [1, 3, 6, 5, 9, 8, 15]; function extractMin(heap) { const min = heap[0]; heap[0] = heap.pop(); let i = 0; while (true) { let left = 2 * i + 1; let right = 2 * i + 2; let smallest = i; if (left < heap.length && heap[left] < heap[smallest]) smallest = left; if (right < heap.length && heap[right] < heap[smallest]) smallest = right; if (smallest === i) break; [heap[i], heap[smallest]] = [heap[smallest], heap[i]]; i = smallest; } return heap; } extractMin(heap); extractMin(heap); console.log(heap);
Attempts:
2 left
💡 Hint
Perform extractMin step-by-step, updating the heap array and bubbling down each time.
✗ Incorrect
First extractMin removes 1, replaces root with 15, bubble down swaps 15 with 3 then 5. Heap becomes [3, 5, 6, 15, 9, 8]. Second extractMin removes 3, replaces root with 8, bubble down swaps 8 with 5. Final heap is [5, 8, 6, 15, 9].