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 Python
heap = [10, 20, 30, 40, 50] # Insert 15 heap.append(15) # Bubble up operation index = len(heap) - 1 while index > 0: parent = (index - 1) // 2 if heap[parent] > heap[index]: heap[parent], heap[index] = heap[index], heap[parent] index = parent else: break print(heap)
Attempts:
2 left
💡 Hint
Remember bubble up swaps the inserted element with its parent if smaller.
✗ Incorrect
The inserted value 15 is appended at the end. It swaps with 30 (its parent) because 15 < 30. No further swaps needed as 15 > 10.
🧠 Conceptual
intermediateWhy does bubble up stop during heap insertion?
In a min-heap insertion, why does the bubble up process stop at some point?
Attempts:
2 left
💡 Hint
Bubble up swaps only if the child is smaller than the parent.
✗ Incorrect
Bubble up stops when the inserted element is no longer smaller than its parent, maintaining the min-heap property.
❓ Predict Output
advancedWhat is the heap after inserting 5 into this max-heap?
Given a max-heap array, what is the heap array after inserting 5 and performing bubble up?
DSA Python
heap = [50, 30, 40, 10, 20] # Insert 5 heap.append(5) # Bubble up for max-heap index = len(heap) - 1 while index > 0: parent = (index - 1) // 2 if heap[parent] < heap[index]: heap[parent], heap[index] = heap[index], heap[parent] index = parent else: break print(heap)
Attempts:
2 left
💡 Hint
Bubble up swaps only if child is greater than parent in max-heap.
✗ Incorrect
5 is smaller than its parent 40, so no swaps happen. The heap remains the same except 5 is added at the end.
🔧 Debug
advancedIdentify the bug in this bubble up code for min-heap insertion
What is the error in this bubble up code snippet for min-heap insertion?
DSA Python
heap = [10, 20, 30, 40, 50] heap.append(15) index = len(heap) - 1 while index > 0: parent = index // 2 if heap[parent] > heap[index]: heap[parent], heap[index] = heap[index], heap[parent] index = parent else: break print(heap)
Attempts:
2 left
💡 Hint
Check how parent index is calculated in a binary heap array.
✗ Incorrect
Parent index in a binary heap is (index - 1) // 2, not index // 2. Using index // 2 causes wrong parent selection.
🚀 Application
expertAfter inserting 25, what is the heap array?
You have a min-heap: [5, 10, 15, 20, 30, 40]. You insert 25 and perform bubble up. What is the heap array after insertion?
DSA Python
heap = [5, 10, 15, 20, 30, 40] heap.append(25) index = len(heap) - 1 while index > 0: parent = (index - 1) // 2 if heap[parent] > heap[index]: heap[parent], heap[index] = heap[index], heap[parent] index = parent else: break print(heap)
Attempts:
2 left
💡 Hint
Bubble up swaps only if the inserted element is smaller than its parent.
✗ Incorrect
25 is inserted at the end. Its parent is 15 which is smaller, so no swaps happen. Heap remains with 25 at the end.
