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 always greater (max-heap) or smaller (min-heap) than its children.
Click to reveal answer
beginner
In a max-heap, when does the bubble up operation stop?
It stops when the inserted element is less than or equal to its parent or when it reaches the root of the heap.
Click to reveal answer
beginner
How do you find the parent index 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
intermediate
What happens if the newly inserted element is larger than its parent in a max-heap during bubble up?
They swap places, and the bubble up continues with the new parent index until the heap property is restored.
Click to reveal answer
beginner
Why is bubble up important for heap insert operation?
Because it maintains the heap structure and order after adding a new element, ensuring efficient retrieval of the max or min element.
Click to reveal answer
What is the first step after inserting a new element at the end of a heap array?
ADo nothing
BRemove the root element
CSort the entire array
DPerform bubble up to restore heap property
✗ Incorrect
After insertion at the end, bubble up moves the element up to maintain heap order.
In a max-heap, if the inserted element is smaller than its parent, what happens during bubble up?
AStop bubbling up
BSwap with parent
CSwap with child
DRemove the element
✗ Incorrect
Bubble up stops because the heap property is satisfied.
How do you calculate the parent index of a node at index 5 in a heap array?
A2
B3
C4
D1
✗ Incorrect
Parent index = Math.floor((5 - 1) / 2) = 2.
What data structure property does bubble up help maintain?
AStack order
BQueue order
CHeap property
DLinked list order
✗ Incorrect
Bubble up restores the heap property after insertion.
Which of these is true about bubble up in a min-heap?
AIt moves the element down
BIt moves the element up if smaller than parent
CIt swaps with the smaller parent
DIt swaps with the larger parent
✗ Incorrect
In a min-heap, bubble up moves the element up if it is smaller than its parent.
Explain the bubble up process during a heap insert operation.
Think about how the new element moves up to keep the heap order.
You got /4 concepts.
Describe how to find the parent of a node in a heap stored as an array and why this is important for bubble up.
Remember the formula for parent index and its role in bubble up.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the bubble up operation when inserting a new element into a max heap?
easy
A. To move the new element up until the heap property is restored
B. To move the new element down to the bottom of the heap
C. To remove the smallest element from the heap
D. To sort all elements in ascending order
Solution
Step 1: Understand heap property
A max heap requires each parent to be greater than or equal to its children.
Step 2: Purpose of bubble up
When a new element is added at the end, bubble up moves it up to restore the max heap property.
Final Answer:
To move the new element up until the heap property is restored -> Option A
Quick Check:
Bubble up fixes heap by moving element up [OK]
Hint: Bubble up moves new element up to fix heap [OK]
Common Mistakes:
Thinking bubble up moves element down
Confusing bubble up with removing elements
Assuming bubble up sorts entire heap
2. Which of the following is the correct syntax to swap two elements at indices i and parentIndex in a JavaScript array heap during bubble up?
easy
A. [heap[i], heap[parentIndex]] = [heap[parentIndex], heap[i]];
B. heap.swap(i, parentIndex);
C. swap(heap[i], heap[parentIndex]);
D. heap[i], heap[parentIndex] = heap[parentIndex], heap[i];
3. Given the max heap 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, 40, 10, 20, 45]
C. [45, 30, 50, 10, 20, 40]
D. [50, 30, 45, 10, 20, 40]
Solution
Step 1: Insert 45 at the end
Heap becomes [50, 30, 40, 10, 20, 45]
Step 2: Bubble up 45
Parent index of 45 (index 5) is 2 (value 40). Since 45 > 40, swap them. New heap: [50, 30, 45, 10, 20, 40]
Final Answer:
[50, 30, 45, 10, 20, 40] -> Option D
Quick Check:
Bubble up swaps 45 with 40 [OK]
Hint: Insert at end, then swap up if bigger than parent [OK]
Common Mistakes:
Not swapping after insertion
Swapping with wrong parent index
Assuming insertion sorts entire heap
4. Consider this bubble up code snippet for a max heap insertion:
while (i > 0 && heap[i] > heap[parentIndex]) {
[heap[i], heap[parentIndex]] = [heap[parentIndex], heap[i]];
i = parentIndex;
parentIndex = Math.floor((i - 1) / 2);
}
What is the bug if parentIndex is not updated inside the loop?
medium
A. Syntax error due to missing semicolon
B. Heap property is restored correctly
C. Loop terminates prematurely after one swap because parentIndex never changes
D. The loop never runs because i is never updated
Solution
Step 1: Understand loop variables
Inside the loop, after swap i = parentIndex, but if parentIndex is not updated, i now equals the unchanged parentIndex.
Step 2: Consequence of not updating parentIndex
The condition heap[i] > heap[parentIndex] becomes heap[pos] > heap[pos] which is false, causing premature loop termination without full restoration.
Final Answer:
Loop terminates prematurely after one swap because parentIndex never changes -> Option C
Quick Check:
Without parentIndex update, i == parentIndex stops loop early [OK]
Hint: Always update parentIndex after swapping [OK]
Common Mistakes:
Forgetting to update parentIndex
Assuming i update is enough
Ignoring loop condition effects
5. You have a min heap represented as [5, 10, 15, 20, 25]. You want to insert 12 and maintain the min heap property using bubble up. Which of the following sequences correctly shows the heap array after each bubble up swap?