Challenge - 5 Problems
Heap Bubble Up 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 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
Attempts:
2 left
💡 Hint
Remember that bubble up swaps the inserted element with its parent while it is smaller.
✗ Incorrect
After inserting 15 at the end, it swaps with 30 (its parent) because 15 < 30, resulting in [10, 20, 15, 40, 50, 30]. No further swaps needed as 15 > 10.
🧠 Conceptual
intermediate1:30remaining
Why does bubble up stop during heap insertion?
In a min-heap insertion, bubble up stops when:
Attempts:
2 left
💡 Hint
Think about the heap property and when it is satisfied.
✗ Incorrect
Bubble up stops when the inserted element is not smaller than its parent, meaning the min-heap property is restored.
🔧 Debug
advanced2: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 } } }
Attempts:
2 left
💡 Hint
Check the comparison operator in the if condition.
✗ Incorrect
In a min-heap, bubble up should swap when the child is smaller than the parent. This code swaps when child is greater, which breaks the heap property.
❓ Predict Output
advanced2: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
Attempts:
2 left
💡 Hint
In max-heap bubble up, swap only if the inserted element is greater than its parent.
✗ Incorrect
5 is less than its parent 40, so no swaps occur. The heap remains [50, 30, 40, 10, 20, 5].
🚀 Application
expert2: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
Attempts:
2 left
💡 Hint
Count each swap as the inserted element moves up the heap.
✗ Incorrect
Insert 3 at index 6. Parent index 2 (value 8), swap 3 and 8 (1 swap). New index 2, parent index 0 (value 4), 3 < 4, swap again (2 swaps). Stop at root.