Challenge - 5 Problems
Heap Insert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the heap array after inserting 15?
Given a max-heap represented as an array, what is the array after inserting the value 15 and performing bubble up?
DSA C
int heap[] = {20, 18, 10, 12, 9, 8}; int size = 6; // Insert 15 and bubble up heap[size] = 15; int i = size; size++; while (i > 0 && heap[(i - 1) / 2] < heap[i]) { int temp = heap[i]; heap[i] = heap[(i - 1) / 2]; heap[(i - 1) / 2] = temp; i = (i - 1) / 2; }
Attempts:
2 left
💡 Hint
Remember to swap the inserted element with its parent while it is greater.
✗ Incorrect
After inserting 15 at the end, it bubbles up swapping with 10 to maintain max-heap property.
❓ Predict Output
intermediateWhat is the heap after inserting 5 into the max-heap?
Given the max-heap array, what is the array after inserting 5 and performing bubble up?
DSA C
int heap[] = {30, 20, 15, 10, 8, 12}; int size = 6; // Insert 5 and bubble up heap[size] = 5; int i = size; size++; while (i > 0 && heap[(i - 1) / 2] < heap[i]) { int temp = heap[i]; heap[i] = heap[(i - 1) / 2]; heap[(i - 1) / 2] = temp; i = (i - 1) / 2; }
Attempts:
2 left
💡 Hint
If the inserted value is smaller than its parent, no swaps happen.
✗ Incorrect
Since 5 is smaller than its parent 12, no bubble up swaps occur.
🔧 Debug
advancedIdentify the error in this bubble up code snippet
What error will this bubble up code cause when inserting a new element?
DSA C
int i = size; while (i > 0 && heap[i] > heap[i / 2]) { int temp = heap[i]; heap[i] = heap[i / 2]; heap[i / 2] = temp; i = i / 2; }
Attempts:
2 left
💡 Hint
Check how parent index is calculated for zero-based arrays.
✗ Incorrect
Parent index should be (i-1)/2, not i/2. Using i/2 can cause wrong parent index and infinite loop.
❓ Predict Output
advancedWhat is the heap array after inserting 25?
Given a max-heap array, what is the array after inserting 25 and performing bubble up?
DSA C
int heap[] = {40, 30, 20, 15, 10, 18}; int size = 6; heap[size] = 25; int i = size; size++; while (i > 0 && heap[(i - 1) / 2] < heap[i]) { int temp = heap[i]; heap[i] = heap[(i - 1) / 2]; heap[(i - 1) / 2] = temp; i = (i - 1) / 2; }
Attempts:
2 left
💡 Hint
Bubble up swaps the inserted element with parents while greater.
✗ Incorrect
25 swaps with 20 at index 2, resulting in [40, 30, 25, 15, 10, 18, 20].
🚀 Application
expertHow many swaps occur when inserting 50 into this heap?
Given the max-heap array below, how many swaps happen during bubble up after inserting 50?
DSA C
int heap[] = {45, 30, 40, 10, 20, 35}; int size = 6; heap[size] = 50; int i = size; size++; int swap_count = 0; while (i > 0 && heap[(i - 1) / 2] < heap[i]) { int temp = heap[i]; heap[i] = heap[(i - 1) / 2]; heap[(i - 1) / 2] = temp; i = (i - 1) / 2; swap_count++; }
Attempts:
2 left
💡 Hint
Count each time the inserted element swaps with its parent.
✗ Incorrect
50 swaps with 40, then 45, totaling 2 swaps.
