0
0
DSA C++programming~20 mins

Heap Insert Operation Bubble Up in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Heap Insert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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 << " ";
A[20, 18, 10, 12, 9, 8, 15]
B[20, 18, 15, 12, 9, 8, 10]
C[20, 18, 10, 15, 9, 8, 12]
D[20, 15, 18, 12, 9, 8, 10]
Attempts:
2 left
💡 Hint
Remember to swap the inserted element with its parent while it is greater.
Predict Output
intermediate
2: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 << " ";
A[30, 20, 15, 10, 8, 5]
B[30, 20, 15, 10, 5, 8]
C[30, 20, 15, 5, 8, 10]
D[30, 20, 15, 10, 8, 10]
Attempts:
2 left
💡 Hint
If the inserted value is smaller than its parent, no swaps happen.
🔧 Debug
advanced
2: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;
    }
}
AThe swap should be done only if heap[index] < heap[parent]
BThe loop condition should be index >= 0
CParent index calculation is wrong; should be (index - 1) / 2
DThe comparison should be heap[index] < heap[parent]
Attempts:
2 left
💡 Hint
Check how parent index is calculated in a zero-based array heap.
🧠 Conceptual
advanced
2: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?
A0
B1
C3
D2
Attempts:
2 left
💡 Hint
Count swaps as the inserted value moves up the tree comparing with parents.
Predict Output
expert
3: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 << " ";
A[5, 10, 15, 20, 30, 40, 25]
B[5, 10, 15, 20, 25, 40, 30]
C[5, 10, 15, 20, 30, 25, 40]
D[5, 10, 15, 20, 25, 30, 40]
Attempts:
2 left
💡 Hint
In a min-heap, bubble up swaps only if the child is smaller than the parent.