Challenge - 5 Problems
Heap Extract Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Extract-Min on a Min-Heap
What is the state of the min-heap array after extracting the minimum element once?
DSA C
int heap[] = {2, 5, 3, 10, 7, 6}; int size = 6; // Extract min operation heap[0] = heap[size - 1]; size--; // Bubble down int i = 0; while (true) { int left = 2 * i + 1; int right = 2 * i + 2; int smallest = i; if (left < size && heap[left] < heap[smallest]) smallest = left; if (right < size && heap[right] < heap[smallest]) smallest = right; if (smallest == i) break; int temp = heap[i]; heap[i] = heap[smallest]; heap[smallest] = temp; i = smallest; } // Print heap for (int j = 0; j < size; j++) { printf("%d ", heap[j]); }
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 the root (2), we replace it with the last element (6). Then we bubble down: 6 swaps with 3 (smaller child), resulting in [3, 5, 6, 10, 7].
❓ Predict Output
intermediateOutput of Extract-Max on a Max-Heap
What is the state of the max-heap array after extracting the maximum element once?
DSA C
int heap[] = {20, 15, 18, 10, 12, 17}; int size = 6; // Extract max operation heap[0] = heap[size - 1]; size--; // Bubble down int i = 0; while (true) { int left = 2 * i + 1; int right = 2 * i + 2; int largest = i; if (left < size && heap[left] > heap[largest]) largest = left; if (right < size && heap[right] > heap[largest]) largest = right; if (largest == i) break; int temp = heap[i]; heap[i] = heap[largest]; heap[largest] = temp; i = largest; } // Print heap for (int j = 0; j < size; j++) { printf("%d ", heap[j]); }
Attempts:
2 left
💡 Hint
Replace the root with the last element and bubble down by swapping with the larger child.
✗ Incorrect
After removing the root (20), replace it with last element (17). Bubble down swaps 17 with 18 (larger child), resulting in [18, 15, 17, 10, 12].
🔧 Debug
advancedIdentify the Bug in Bubble Down for Min-Heap
What error will this bubble down code cause when extracting min from a min-heap?
DSA C
int heap[] = {1, 3, 6, 5, 9, 8}; int size = 6; heap[0] = heap[size - 1]; size--; int i = 0; while (i < size) { int left = 2 * i + 1; int right = 2 * i + 2; int smallest = i; if (left < size && heap[left] < heap[smallest]) smallest = left; if (right < size && heap[right] < heap[smallest]) smallest = right; if (smallest == i) break; int temp = heap[i]; heap[i] = heap[smallest]; heap[smallest] = temp; i = smallest; }
Attempts:
2 left
💡 Hint
Check the conditions for left and right child indices compared to size.
✗ Incorrect
The conditions use 'left <= size' and 'right <= size', but valid indices are less than size. This causes out-of-bounds access.
🧠 Conceptual
advancedWhy Bubble Down is Needed After Extracting Root in Heap?
Why do we need to perform bubble down after replacing the root with the last element in a heap?
Attempts:
2 left
💡 Hint
Think about what happens when the root is replaced by the last element.
✗ Incorrect
Replacing the root with the last element may break the heap property at the root. Bubble down moves this element down to restore the heap order.
❓ Predict Output
expertFinal Heap State After Multiple Extract-Min Operations
Given the min-heap array [4, 7, 6, 10, 9, 8], what is the heap array after extracting the minimum element twice (two extract-min operations with bubble down each time)?
DSA C
int heap[] = {4, 7, 6, 10, 9, 8}; int size = 6; // First extract-min heap[0] = heap[size - 1]; size--; int i = 0; while (true) { int left = 2 * i + 1; int right = 2 * i + 2; int smallest = i; if (left < size && heap[left] < heap[smallest]) smallest = left; if (right < size && heap[right] < heap[smallest]) smallest = right; if (smallest == i) break; int temp = heap[i]; heap[i] = heap[smallest]; heap[smallest] = temp; i = smallest; } // Second extract-min heap[0] = heap[size - 1]; size--; i = 0; while (true) { int left = 2 * i + 1; int right = 2 * i + 2; int smallest = i; if (left < size && heap[left] < heap[smallest]) smallest = left; if (right < size && heap[right] < heap[smallest]) smallest = right; if (smallest == i) break; int temp = heap[i]; heap[i] = heap[smallest]; heap[smallest] = temp; i = smallest; } // Print heap for (int j = 0; j < size; j++) { printf("%d ", heap[j]); }
Attempts:
2 left
💡 Hint
Perform extract-min twice carefully, updating size and bubbling down each time.
✗ Incorrect
First extract-min removes 4, replaces root with 8, bubble down swaps 8 with 6, resulting in [6,7,8,10,9]. Second extract-min removes 6, replaces root with 9, bubble down swaps 9 with 7, resulting in [7,9,8,10].
