Challenge - 5 Problems
Heap Mastery Badge
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 min-heap represented as an array, what is the state of the heap array after inserting the value 15 and performing bubble up?
DSA C#
int[] heap = {10, 20, 30, 40, 50}; // Insert 15 and bubble up // What is the heap array now?
Attempts:
2 left
💡 Hint
Remember that bubble up swaps the inserted element with its parent while it is smaller.
✗ Incorrect
Inserting 15 adds it at the end: [10, 20, 30, 40, 50, 15]. Bubble up compares 15 with its parent 20 and swaps them, resulting in [10, 15, 30, 40, 50, 20].
❓ Predict Output
intermediateWhat is the heap array after inserting 5?
Given a min-heap array, what is the heap array after inserting 5 and performing bubble up?
DSA C#
int[] heap = {10, 15, 30, 40, 50, 20}; // Insert 5 and bubble up // What is the heap array now?
Attempts:
2 left
💡 Hint
Bubble up swaps repeatedly until the inserted value is not smaller than its parent.
✗ Incorrect
Insert 5 at the end: [10, 15, 30, 40, 50, 20, 5]. Bubble up swaps 5 with 30, then with 15, resulting in [10, 5, 15, 40, 50, 20, 30], then swaps 5 with 10, final heap: [5, 10, 15, 40, 50, 20, 30].
🔧 Debug
advancedWhy does this bubble up code fail to maintain heap property?
Consider this bubble up code snippet for a min-heap insert operation. Why does it fail to maintain the heap property?
DSA C#
void BubbleUp(int[] heap, int index) { while (index > 0) { int parent = (index - 1) / 2; if (heap[index] < heap[parent]) { int temp = heap[index]; heap[index] = heap[parent]; heap[parent] = temp; } else { break; } index = parent; } }
Attempts:
2 left
💡 Hint
Check when the loop should stop during bubble up.
✗ Incorrect
The code swaps when needed but always moves index to parent, even if no swap occurred. It should stop when no swap is needed to avoid unnecessary moves.
🧠 Conceptual
advancedHow many swaps happen when inserting 1 into this min-heap?
Given the min-heap array [5, 10, 15, 40, 50, 20, 30], how many swaps occur during bubble up after inserting 1?
DSA C#
int[] heap = {5, 10, 15, 40, 50, 20, 30}; // Insert 1 and bubble up // How many swaps happen?
Attempts:
2 left
💡 Hint
Count swaps as 1 moves up comparing with parents until heap property holds.
✗ Incorrect
Insert 1 at index 7. Swaps: 1 with 30 (1), then with 15 (2), then with 5 (3). Stops at root. Total 3 swaps.
🚀 Application
expertWhat is the heap array after inserting 25 into this max-heap?
Given a max-heap array [50, 40, 45, 20, 30, 35], what is the heap array after inserting 25 and performing bubble up?
DSA C#
int[] heap = {50, 40, 45, 20, 30, 35}; // Insert 25 and bubble up // What is the heap array now?
Attempts:
2 left
💡 Hint
In max-heap bubble up, swap only if inserted value is greater than parent.
✗ Incorrect
Insert 25 at end: [50, 40, 45, 20, 30, 35, 25]. Bubble up compares 25 with parent 45; 25 < 45, no swap. Heap unchanged except new element at end.
