0
0
DSA Goprogramming~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Heap Bubble Up 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 min-heap represented as an array, what is the state of the heap array after inserting the value 15 and performing bubble up?
DSA Go
heap := []int{10, 20, 30, 40, 50}
// Insert 15 and bubble up
heap = append(heap, 15)
// Bubble up logic here
A[10, 20, 15, 40, 50, 30]
B[10, 20, 30, 40, 50, 15]
C[10, 15, 30, 40, 50, 20]
D[15, 10, 30, 40, 50, 20]
Attempts:
2 left
💡 Hint
Remember that bubble up swaps the inserted element with its parent while it is smaller.
🧠 Conceptual
intermediate
1:30remaining
Why does bubble up stop during heap insertion?
In a min-heap insertion, bubble up stops when:
AThe inserted element is greater than or equal to its parent
BThe inserted element reaches the last index
CThe inserted element is smaller than its parent
DThe heap array is full
Attempts:
2 left
💡 Hint
Think about the heap property and when it is satisfied.
🔧 Debug
advanced
2:30remaining
Find the error in this bubble up code snippet
What error will this Go code cause during bubble up in a min-heap insertion?
DSA Go
func bubbleUp(heap []int, index int) {
    for index > 0 {
        parent := (index - 1) / 2
        if heap[index] > heap[parent] {
            heap[index], heap[parent] = heap[parent], heap[index]
            index = parent
        } else {
            break
        }
    }
}
AThe code swaps when the child is smaller than the parent, which is correct
BThe code causes an index out of range error
CThe code never stops and causes an infinite loop
DThe code swaps when the child is greater than the parent, breaking min-heap property
Attempts:
2 left
💡 Hint
Check the comparison operator in the if condition.
Predict Output
advanced
2:00remaining
What is the heap array after inserting 5 into this max-heap?
Given a max-heap array, what is the state after inserting 5 and performing bubble up?
DSA Go
heap := []int{50, 30, 40, 10, 20}
// Insert 5 and bubble up
heap = append(heap, 5)
// Bubble up logic here
A[50, 30, 40, 10, 20, 10]
B[50, 30, 40, 10, 20, 50]
C[50, 30, 40, 10, 20, 5]
D[5, 50, 40, 10, 20, 30]
Attempts:
2 left
💡 Hint
In max-heap bubble up, swap only if the inserted element is greater than its parent.
🚀 Application
expert
2:30remaining
How many swaps occur when inserting 3 into this min-heap?
Given the min-heap array [4, 7, 8, 10, 15, 9], how many swaps happen when inserting 3 and bubbling up?
DSA Go
heap := []int{4, 7, 8, 10, 15, 9}
// Insert 3 and bubble up
A3
B2
C1
D0
Attempts:
2 left
💡 Hint
Count each swap as the inserted element moves up the heap.