What is the purpose of the 'bubble down' operation in a heap after extracting the min or max element?
The 'bubble down' operation restores the heap property by moving the new root element down the tree until it is smaller (min-heap) or larger (max-heap) than its children.
Click to reveal answer
beginner
In a min-heap, after extracting the minimum element, which element replaces the root before bubbling down?
The last element in the heap replaces the root before the bubble down process starts.
Click to reveal answer
intermediate
What condition stops the bubble down process in a heap?
Bubble down stops when the current node is smaller (min-heap) or larger (max-heap) than both its children or when it reaches a leaf node.
Click to reveal answer
intermediate
Why do we swap the current node with the smaller (min-heap) or larger (max-heap) child during bubble down?
Swapping ensures the heap property is maintained by moving the out-of-place element down to its correct position.
Click to reveal answer
advanced
Show the Rust code snippet for the bubble down function in a min-heap.
fn bubble_down(heap: &mut Vec<i32>, mut index: usize) {
let length = heap.len();
loop {
let left = 2 * index + 1;
let right = 2 * index + 2;
let mut smallest = index;
if left < length && heap[left] < heap[smallest] {
smallest = left;
}
if right < length && heap[right] < heap[smallest] {
smallest = right;
}
if smallest == index {
break;
}
heap.swap(index, smallest);
index = smallest;
}
}
Click to reveal answer
What element replaces the root in a heap after extracting the min or max element?
AThe last element in the heap
BThe second smallest element
CA new random element
DThe root stays empty
✗ Incorrect
After extraction, the last element moves to the root before bubble down restores the heap.
When does the bubble down process stop in a min-heap?
AAfter exactly three swaps
BWhen the current node is larger than both children
CWhen the current node is smaller than both children or no children exist
DWhen the heap is empty
✗ Incorrect
Bubble down stops when the heap property is restored or the node is a leaf.
In a max-heap, bubble down swaps the current node with which child?
AThe larger child
BThe smaller child
CThe left child always
DNo swap is done
✗ Incorrect
To maintain max-heap property, swap with the larger child.
What is the time complexity of the bubble down operation in a heap of size n?
AO(1)
BO(n log n)
CO(n)
DO(log n)
✗ Incorrect
Bubble down moves down the height of the heap, which is log n.
Which of these is NOT a step in the extract min or max operation in a heap?
AReplace root with last element
BInsert a new element at the root
CBubble down the new root
DRemove the extracted element
✗ Incorrect
Insertion is a separate operation, not part of extract min or max.
Explain the steps involved in extracting the minimum element from a min-heap and how bubble down restores the heap property.
Think about what happens to the root and how the heap stays ordered.
You got /5 concepts.
Describe how the bubble down operation differs between a min-heap and a max-heap.
Focus on the comparison direction for swapping.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the bubble down operation in a min-heap after extracting the minimum element?
easy
A. To insert a new element at the bottom of the heap
B. To remove all elements smaller than the root
C. To swap the last element with the root without any further changes
D. To restore the heap property by moving the root element down to its correct position
Solution
Step 1: Understand extract-min operation
Extracting the minimum removes the root, so the last element moves to the root.
Step 2: Purpose of bubble down
Bubble down moves this new root down to restore the min-heap property by swapping with smaller children.
Final Answer:
To restore the heap property by moving the root element down to its correct position -> Option D
Quick Check:
Bubble down fixes heap after extract-min [OK]
Hint: Bubble down fixes heap from root after extract-min [OK]
Common Mistakes:
Thinking bubble down inserts new elements
Assuming bubble down removes elements
Confusing bubble down with bubble up
2. Which of the following Rust function signatures correctly represents a bubble down operation for a min-heap stored in a vector?
easy
A. fn bubble_down(&heap: Vec<i32>, index: usize)
B. fn bubble_down(heap: &mut Vec<i32>, index: usize)
C. fn bubble_down(heap: Vec<i32>, index: usize) -> Vec<i32>
D. fn bubble_down(heap: &Vec<i32>, index: usize)
Solution
Step 1: Identify mutability needs
Bubble down modifies the heap, so it needs a mutable reference to the vector.
If bubble_down correctly restores the min-heap property, what will be the printed vector?
medium
A. [3, 5, 8, 6, 9]
B. [6, 3, 8, 5, 9]
C. [3, 5, 6, 8, 9]
D. [5, 3, 6, 8, 9]
Solution
Step 1: Understand initial heap and pop
Initial heap: [1,3,6,5,9,8]. After pop(), last element 8 is removed and placed at root: heap[0] = 8, heap = [8,3,6,5,9]
Step 2: Bubble down from root
Compare 8 with children 3 and 6; smallest is 3, swap 8 and 3: [3,8,6,5,9]. Next, bubble down 8 at index 1 with children 5 and 9; smallest is 5, swap: [3,5,6,8,9]. No further swaps needed.
Final Answer:
[3, 5, 6, 8, 9] -> Option C
Quick Check:
Bubble down swaps to restore min-heap [OK]
Hint: Replace root with last, then bubble down to fix heap [OK]
Common Mistakes:
Not swapping with smallest child
Stopping bubble down too early
Incorrect pop and root replacement
4. Given this Rust bubble down function snippet for a max-heap, identify the bug:
fn bubble_down(heap: &mut Vec<i32>, mut index: usize) {
let len = heap.len();
loop {
let left = 2 * index + 1;
let right = 2 * index + 2;
let mut largest = index;
if left < len && heap[left] > heap[largest] {
largest = left;
}
if right < len && heap[right] > heap[largest] {
largest = right;
}
if largest == index {
break;
}
heap.swap(index, largest);
// Missing update of index here
}
}
medium
A. The index variable is not updated after swapping, causing an infinite loop
B. The function uses a min-heap condition instead of max-heap
C. The left and right child indices are calculated incorrectly
D. The swap function is used incorrectly
Solution
Step 1: Analyze loop behavior
After swapping, the index must move to the child's position to continue bubbling down.
Step 2: Identify missing update
The code does not update index = largest after swap, so loop repeats with same index causing infinite loop.
Final Answer:
The index variable is not updated after swapping, causing an infinite loop -> Option A
Quick Check:
Update index after swap to continue bubble down [OK]
Hint: Always update index after swap in bubble down loop [OK]
Common Mistakes:
Forgetting to update index after swap
Mixing min-heap and max-heap conditions
Incorrect child index calculations
5. You have a max-heap represented as [50, 30, 40, 10, 20, 35]. After extracting the maximum element (50), you replace the root with the last element and perform bubble down. What is the final heap array after bubble down completes?
hard
A. [40, 30, 35, 10, 20]
B. [40, 30, 10, 20, 35]
C. [35, 30, 40, 10, 20]
D. [35, 30, 20, 10, 40]
Solution
Step 1: Replace root with last element
Remove 50, move last element 35 to root: [35, 30, 40, 10, 20]
Step 2: Bubble down from root
Compare 35 with children 30 and 40; largest is 40, swap: [40, 30, 35, 10, 20]. Next, bubble down 35 at index 2 with children none (leaf), stop.
Final Answer:
[40, 30, 35, 10, 20] -> Option A
Quick Check:
Bubble down swaps root with largest child until heap restored [OK]
Hint: Replace root with last, bubble down swapping with largest child [OK]
Common Mistakes:
Not swapping with largest child
Stopping bubble down too early
Incorrect root replacement
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.