Challenge - 5 Problems
Heap Insert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What 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++
std::vector<int> heap = {20, 18, 10, 12, 9, 8}; int value = 15; // Insert value and bubble up heap.push_back(value); int i = heap.size() - 1; while (i > 0) { int parent = (i - 1) / 2; if (heap[i] <= heap[parent]) break; std::swap(heap[i], heap[parent]); i = parent; } for (int v : heap) std::cout << v << " ";
Attempts:
2 left
💡 Hint
Remember to swap the inserted element with its parent while it is greater.
✗ Incorrect
The inserted value 15 is placed at the end. It swaps with 10 (parent) because 15 > 10, then stops because 15 <= 20 (new parent). The final heap array is [20, 18, 15, 12, 9, 8, 10].
❓ Predict Output
intermediate2:00remaining
What is the heap after inserting 5 into this max-heap?
Starting with the max-heap array [30, 20, 15, 10, 8], what is the array after inserting 5 and performing bubble up?
DSA C++
std::vector<int> heap = {30, 20, 15, 10, 8}; int value = 5; heap.push_back(value); int i = heap.size() - 1; while (i > 0) { int parent = (i - 1) / 2; if (heap[i] <= heap[parent]) break; std::swap(heap[i], heap[parent]); i = parent; } for (int v : heap) std::cout << v << " ";
Attempts:
2 left
💡 Hint
If the inserted value is smaller than its parent, no swaps happen.
✗ Incorrect
The inserted value 5 is smaller than its parent 15, so no bubble up swaps occur. The heap array becomes [30, 20, 15, 10, 8, 5].
🔧 Debug
advanced2:00remaining
Identify the bug in this bubble up code snippet
What is the error in this bubble up code for a max-heap insert?
DSA C++
void bubbleUp(std::vector<int>& heap, int index) { while (index > 0) { int parent = (index - 1) / 2; if (heap[index] <= heap[parent]) break; std::swap(heap[index], heap[parent]); index = parent; } }
Attempts:
2 left
💡 Hint
Check how parent index is calculated in a zero-based array heap.
✗ Incorrect
In zero-based indexing, parent of node at index i is (i - 1) / 2. Using i / 2 is incorrect and causes wrong parent calculation.
🧠 Conceptual
advanced2:00remaining
How many swaps occur when inserting 50 into this max-heap?
Given the max-heap array [40, 30, 20, 15, 10, 5], how many swaps happen when inserting 50 and bubbling up?
Attempts:
2 left
💡 Hint
Count swaps as the inserted value moves up the tree comparing with parents.
✗ Incorrect
Insert 50 at index 6. Parent at index 2 (20), swap 1. New index 2, parent at index 0 (40), swap 2. New index 0, no parent. Total 2 swaps.
❓ Predict Output
expert3:00remaining
What is the heap array after inserting 25 into this min-heap?
Given a min-heap array [5, 10, 15, 20, 30, 40], what is the array after inserting 25 and performing bubble up?
DSA C++
std::vector<int> heap = {5, 10, 15, 20, 30, 40}; int value = 25; heap.push_back(value); int i = heap.size() - 1; while (i > 0) { int parent = (i - 1) / 2; if (heap[i] >= heap[parent]) break; std::swap(heap[i], heap[parent]); i = parent; } for (int v : heap) std::cout << v << " ";
Attempts:
2 left
💡 Hint
In a min-heap, bubble up swaps only if the child is smaller than the parent.
✗ Incorrect
25 is inserted at the end. Its parent is 15, and since 25 > 15, no swaps occur. The heap remains [5, 10, 15, 20, 30, 40, 25].