What is the purpose of the bubble up operation in a heap insert?
The bubble up operation moves the newly inserted element up the heap to restore the heap property, ensuring the parent node is smaller (min-heap) or larger (max-heap) than its children.
Click to reveal answer
beginner
In a min-heap, when does the bubble up operation stop?
It stops when the inserted element is greater than or equal to its parent or when it reaches the root of the heap.
Click to reveal answer
intermediate
What is the time complexity of the bubble up operation in a heap?
The time complexity is O(log n), where n is the number of elements in the heap, because the element moves up at most the height of the heap.
Click to reveal answer
beginner
Which index calculation is used to find the parent of a node at index i in a heap array?
The parent index is calculated as Math.floor((i - 1) / 2).
Click to reveal answer
beginner
Explain the bubble up process with an example of inserting 5 into a min-heap [2, 4, 7].
Insert 5 at the end: [2, 4, 7, 5]. Compare 5 with parent 4. Since 5 > 4, no swap needed. Bubble up stops. Final heap: [2, 4, 7, 5].
Click to reveal answer
What does the bubble up operation do after inserting a new element in a min-heap?
ARemoves the smallest element
BMoves the element up until it is larger than its parent
CMoves the element down to maintain heap property
DMoves the element up until it is no longer smaller than its parent
✗ Incorrect
In a min-heap, bubble up moves the new element up until it is no longer smaller than its parent.
What is the parent index of the node at index 6 in a heap array?
When inserting into a max-heap, bubble up continues as long as the inserted element is:
ALarger than its parent
BSmaller than its parent
CEqual to its parent
DAt the root
✗ Incorrect
In a max-heap, bubble up moves the element up while it is larger than its parent.
What is the worst-case time complexity of bubble up in a heap with n elements?
AO(log n)
BO(n)
CO(1)
DO(n log n)
✗ Incorrect
Bubble up moves up the height of the heap, which is log n.
If the newly inserted element is at the root after bubble up, what does it mean?
AHeap property is violated
BThe element is the smallest or largest
CThe heap is empty
DHeap property is restored
✗ Incorrect
If bubble up reaches root, the element is the smallest (min-heap) or largest (max-heap).
Describe the bubble up operation during heap insertion and why it is necessary.
Think about how the heap keeps order after adding a new value.
You got /4 concepts.
Explain how to find the parent index of a node in a heap stored as an array and how it is used in bubble up.
Remember the formula involves dividing the current index.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the bubble up operation during a heap insert?
easy
A. To swap all elements randomly in the heap
B. To remove the root element from the heap
C. To sort all elements in the heap in ascending order
D. To restore the heap property by moving the new element up if it is smaller (min-heap) or larger (max-heap) than its parent
Solution
Step 1: Understand the heap property
In a min-heap, each parent node must be smaller than its children. In a max-heap, each parent must be larger.
Step 2: Purpose of bubble up
When inserting a new element, it is placed at the end. Bubble up moves it up the tree to restore the heap property if violated.
Final Answer:
To restore the heap property by moving the new element up if it is smaller (min-heap) or larger (max-heap) than its parent -> Option D
Quick Check:
Bubble up fixes heap order after insert [OK]
Hint: Bubble up fixes heap order by moving new element up [OK]
Common Mistakes:
Confusing bubble up with removing elements
Thinking bubble up sorts the entire heap
Assuming bubble up swaps elements randomly
2. Which of the following TypeScript code snippets correctly performs the bubble up step in a min-heap after inserting a new element at the end?
easy
A. while (index > 0 && heap[index] < heap[parentIndex]) { swap(heap, index, parentIndex); index = parentIndex; }
B. while (index > 0 && heap[index] > heap[parentIndex]) { swap(heap, index, parentIndex); index = parentIndex; }
C. for (let i = 0; i < heap.length; i++) { if (heap[i] < heap[i+1]) swap(heap, i, i+1); }
D. if (heap[index] > heap[parentIndex]) { swap(heap, index, parentIndex); }
Solution
Step 1: Identify min-heap bubble up condition
In a min-heap, bubble up swaps while the child is smaller than the parent.
Step 2: Check code correctness
while (index > 0 && heap[index] < heap[parentIndex]) { swap(heap, index, parentIndex); index = parentIndex; } uses a while loop with condition heap[index] < heap[parentIndex], correctly swapping and updating index.
Final Answer:
while (index > 0 && heap[index] < heap[parentIndex]) { swap(heap, index, parentIndex); index = parentIndex; } -> Option A
Quick Check:
Min-heap bubble up swaps smaller child up [OK]
Hint: Bubble up swaps while child < parent in min-heap [OK]
Common Mistakes:
Using > instead of < for min-heap
Using for loop instead of while
Not updating index after swap
3. Given a max-heap represented as an array: [50, 30, 40, 10, 20], what will be the heap array after inserting 45 and performing bubble up?
medium
A. [50, 45, 40, 10, 20, 30]
B. [50, 30, 45, 10, 20, 40]
C. [50, 30, 40, 10, 20, 45]
D. [45, 30, 50, 10, 20, 40]
Solution
Step 1: Insert 45 at the end
Array becomes [50, 30, 40, 10, 20, 45]
Step 2: Bubble up 45 in max-heap
Parent index of 45 (index 5) is 2 (value 40). Since 45 > 40, swap. New array: [50, 30, 45, 10, 20, 40]. Now index 2's parent is 0 (value 50). 45 < 50, stop bubbling up.
Final Answer:
[50, 30, 45, 10, 20, 40] -> Option B
Quick Check:
Bubble up swaps 45 with 40 only [OK]
Hint: Insert at end, swap up while child > parent in max-heap [OK]
Common Mistakes:
Not swapping 45 with 40
Swapping 45 with 30 instead of 40
Stopping bubble up too early
4. Consider this bubble up code snippet for a min-heap insert:
while (index > 0 && heap[index] > heap[parentIndex]) {
swap(heap, index, parentIndex);
index = parentIndex;
}
What is the error in this code?
medium
A. The parentIndex is not updated inside the loop
B. The swap function is called incorrectly
C. The comparison operator should be '<' to maintain min-heap property
D. The loop should run while index < 0
Solution
Step 1: Check comparison for min-heap bubble up
In a min-heap, bubble up swaps when child is smaller than parent, so condition should be heap[index] < heap[parentIndex].
Step 2: Identify error in code
The code uses '>' which is incorrect for min-heap bubble up, causing wrong swaps.
Final Answer:
The comparison operator should be '<' to maintain min-heap property -> Option C
Quick Check:
Min-heap bubble up uses < comparison [OK]
Hint: Min-heap bubble up swaps when child < parent [OK]
Common Mistakes:
Using > instead of < in condition
Not updating parentIndex after swap
Loop condition index > 0 is correct, not < 0
5. You have a min-heap represented as [5, 10, 15, 20, 25, 30]. You insert 12 and perform bubble up. After that, you insert 3 and perform bubble up again. What is the final heap array?
hard
A. [3, 5, 12, 10, 25, 30, 15, 20]
B. [3, 5, 15, 10, 25, 30, 12, 20]
C. [3, 5, 12, 20, 25, 30, 15, 10]
D. [3, 5, 15, 20, 25, 30, 12, 10]
Solution
Step 1: Insert 12 and bubble up
Insert 12 at index 6: [5,10,15,20,25,30,12]. Parent of index 6 is 2 (value 15). Since 12 < 15, swap: [5,10,12,20,25,30,15]. Parent of index 2 is 0 (value 5). 12 > 5, stop.
Step 2: Insert 3 and bubble up
Insert 3 at index 7: [5,10,12,20,25,30,15,3]. Parent of index 7 is 3 (value 20). 3 < 20, swap: [5,10,12,3,25,30,15,20]. Parent of index 3 is 1 (value 10). 3 < 10, swap: [5,3,12,10,25,30,15,20]. Parent of index 1 is 0 (value 5). 3 < 5, swap: [3,5,12,10,25,30,15,20]. Now at root, stop.
Final Answer:
[3, 5, 12, 10, 25, 30, 15, 20] -> Option A
Quick Check:
Bubble up swaps until heap property restored [OK]
Hint: Bubble up each insert until parent smaller in min-heap [OK]
Common Mistakes:
Not swapping 3 all the way to root
Incorrect parent index calculation
Stopping bubble up too early
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.