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 root element down the tree, swapping it with its smaller (min-heap) or larger (max-heap) child until the heap order is correct.
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 begins to restore the heap property.
Click to reveal answer
intermediate
What condition determines when to stop 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 has no children.
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 a position where it is correctly ordered relative to its children.
Click to reveal answer
beginner
Show the TypeScript signature of a bubble down function for a min-heap.
function bubbleDown(heap: number[], index: number): void
Click to reveal answer
What element is removed during the extract operation in a min-heap?
AThe largest element
BThe last element
CThe root element (minimum)
DA random element
✗ Incorrect
In a min-heap, the root element is the minimum and is removed during extraction.
After removing the root in a max-heap, which element temporarily replaces the root before bubbling down?
AA new element
BThe smallest element
CThe second largest element
DThe last element in the heap
✗ Incorrect
The last element replaces the root temporarily before bubble down restores the heap.
During bubble down in a min-heap, you swap the current node with:
AThe smaller child
BThe larger child
CThe parent node
DNo one, you only move up
✗ Incorrect
In a min-heap, bubble down swaps with the smaller child to maintain heap order.
When does the bubble down process stop?
AAfter one swap
BWhen the node is smaller (min-heap) or larger (max-heap) than its children or has no children
CWhen the heap is empty
DWhen the node reaches the root
✗ Incorrect
Bubble down stops when the heap property is restored or no children exist.
What is the time complexity of the bubble down operation in a heap?
AO(log n)
BO(n)
CO(1)
DO(n log n)
✗ Incorrect
Bubble down takes O(log n) time because it moves down the height of the heap.
Explain step-by-step how the bubble down operation works after extracting the root in a min-heap.
Think about how the heap property is maintained by moving the root down.
You got /4 concepts.
Describe why bubble down is necessary after extracting the min or max element from a heap.
Consider what happens to the heap shape and order after extraction.
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 delete the last element in the heap
B. To insert a new element at the bottom of the heap
C. To swap the minimum element with the maximum element
D. To restore the heap property by moving the root element down to its correct position
Solution
Step 1: Understand extract-min operation
When the minimum element (root) is removed, the last element replaces the root.
Step 2: Apply bubble down to restore heap
The bubble down moves this element down the tree to restore the min-heap property.
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 after removing root [OK]
Common Mistakes:
Confusing bubble down with insertion
Thinking bubble down swaps min and max
Assuming bubble down deletes elements
2. Which of the following TypeScript code snippets correctly swaps two elements in a heap array during bubble down?
Hint: Use temp variable for swapping in TypeScript [OK]
Common Mistakes:
Using Python tuple swap syntax in TypeScript
Calling undefined swap function
Overwriting values without temp variable
3. Consider the min-heap array [2, 5, 3, 10, 7]. After extracting the minimum element and performing bubble down, what is the resulting heap array?
medium
A. [3, 5, 7, 10]
B. [3, 7, 5, 10]
C. [5, 7, 3, 10]
D. [7, 5, 3, 10]
Solution
Step 1: Remove root and replace with last element
Remove 2, replace root with 7: [7, 5, 3, 10]
Step 2: Bubble down 7 to restore min-heap
Compare 7 with children 5 and 3; swap with smallest child 3: [3, 5, 7, 10]
Final Answer:
[3, 5, 7, 10] -> Option A
Quick Check:
Extract min and bubble down result = [3, 5, 7, 10] [OK]
Hint: Replace root with last, then bubble down [OK]
Common Mistakes:
Not replacing root with last element
Swapping with wrong child during bubble down
Forgetting to remove last element
4. The following TypeScript code attempts to bubble down an element in a max-heap but does not work correctly. What is the main bug?
function bubbleDown(heap: number[], i: number) {
const left = 2 * i + 1;
const right = 2 * i + 2;
let largest = i;
if (left < heap.length && heap[left] > heap[largest]) {
largest = left;
}
if (right < heap.length && heap[right] > heap[largest]) {
largest = right;
}
if (largest !== i) {
heap[i] = heap[largest];
heap[largest] = heap[i];
bubbleDown(heap, largest);
}
}
medium
A. Swapping elements overwrites values incorrectly without a temporary variable
B. The indices for left and right children are calculated incorrectly
C. The recursive call should be bubbleUp instead of bubbleDown
D. The condition to check children is reversed for max-heap
Solution
Step 1: Analyze swap logic in code
The swap uses heap[i] = heap[largest]; heap[largest] = heap[i]; which overwrites heap[i] before assigning heap[largest].
Step 2: Identify correct swap method
Swapping requires a temporary variable to avoid overwriting values.
Final Answer:
Swapping elements overwrites values incorrectly without a temporary variable -> Option A
Quick Check:
Swap needs temp variable to avoid overwrite [OK]
Hint: Always use temp variable when swapping array elements [OK]
Common Mistakes:
Overwriting values during swap without temp
Confusing bubbleDown with bubbleUp
Incorrect child index calculations
5. You have a max-heap represented by the array [50, 30, 40, 10, 20, 35]. After extracting the maximum element and performing bubble down, what is the final heap array?
hard
A. [35, 30, 40, 10, 20]
B. [40, 30, 20, 10, 35]
C. [40, 30, 35, 10, 20]
D. [30, 20, 35, 10, 40]
Solution
Step 1: Remove max (root) and replace with last element