What is the purpose of the 'bubble down' operation in a heap after extracting the min or max?
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.
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 has no children.
Click to reveal answer
intermediate
Why do we swap the root with the last element before removing it in heap extraction?
Swapping the root with the last element allows easy removal of the root (min or max) while keeping the heap complete, then bubble down fixes the heap order.
Click to reveal answer
advanced
Show the bubble down steps for extracting min from this min-heap: [2, 5, 3, 10, 7]
1. Remove 2 (root). 2. Move last element 7 to root: [7, 5, 3, 10]. 3. Compare 7 with children 5 and 3. 4. Swap 7 with smaller child 3: [3, 5, 7, 10]. 5. 7 has no smaller children, stop.
Click to reveal answer
What element replaces the root in a max-heap after extracting the maximum?
AThe last element in the heap
BThe smallest child of the root
CThe second largest element
DA new random element
✗ Incorrect
After extracting the max (root), the last element moves to the root before bubble down.
When does the bubble down operation stop in a min-heap?
AWhen the current node is smaller than its children
BBoth A and D
CWhen the current node is larger than its children
DWhen the current node has no children
✗ Incorrect
Bubble down stops when the node is smaller than children or has no children.
Which of these is NOT a step in extracting min from a min-heap?
ASwap root with last element
BBubble down the new root
CRemove last element (original root)
DBubble up the new root
✗ Incorrect
Bubble up is not used after extraction; bubble down restores heap order.
In a max-heap, bubble down swaps the root with which child?
AThe larger child
BThe smaller child
CThe left child always
DThe right child always
✗ Incorrect
Bubble down swaps with the larger child to maintain max-heap property.
What is the time complexity of bubble down in a heap of size n?
AO(n)
BO(1)
CO(log n)
DO(n log n)
✗ Incorrect
Bubble down takes O(log n) time because it moves down the height of the heap.
Explain the steps involved in extracting the minimum element from a min-heap and how bubble down restores the heap.
Think about how the heap stays complete and ordered after removal.
You got /6 concepts.
Describe the difference between bubble down in a min-heap and a max-heap during extraction.
Focus on which child is chosen 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 restore the heap property by moving the root element down to its correct position
B. To insert a new element at the bottom of the heap
C. To swap the last element with the root without any further changes
D. To remove all elements smaller than the root
Solution
Step 1: Understand the extract min operation
When we remove the minimum element (root) from a min-heap, the last element is moved to the root position.
Step 2: Apply bubble down to restore heap property
The bubble down operation moves this new root element down the tree by swapping it with the smaller child until the heap property is restored.
Final Answer:
To restore the heap property by moving the root element down to its correct position -> Option A
Quick Check:
Bubble down fixes heap after extract min [OK]
Hint: Bubble down fixes heap by pushing root down [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 C# code snippets correctly swaps the root element with its smaller child during bubble down in a min-heap?
easy
A. int temp = heap[i]; heap[i] = heap[child]; heap[child] = heap[i];
B. heap[i] = heap[child]; heap[child] = heap[i];
C. swap(heap[i], heap[child]);
D. int temp = heap[i]; heap[i] = heap[child]; heap[child] = temp;
Solution
Step 1: Identify correct swap syntax in C#
int temp = heap[i]; heap[i] = heap[child]; heap[child] = temp; uses a temporary variable to swap two elements correctly, which is the standard way in C#.
Step 2: Check other options for correctness
B: heap[i] = heap[child]; heap[child] = heap[i]; assigns values incorrectly causing data loss. C: swap(heap[i], heap[child]); is invalid syntax in C#. D: int temp = heap[i]; heap[i] = heap[child]; heap[child] = heap[i]; uses temp but assigns heap[child] the updated heap[i] value, causing no swap and data loss.
Final Answer:
int temp = heap[i]; heap[i] = heap[child]; heap[child] = temp; -> Option D
Quick Check:
Use temp variable to swap in C# [OK]
Hint: Use temp variable to swap array elements [OK]
Common Mistakes:
Trying to swap without temp variable
Using invalid syntax like swap() function
Forgetting to use temp in the final assignment
3. Given the min-heap array [2, 5, 3, 10, 7], after extracting the minimum element and performing bubble down, what will be the resulting heap array?
medium
A. [5, 7, 3, 10]
B. [3, 5, 7, 10]
C. [3, 7, 5, 10]
D. [5, 3, 7, 10]
Solution
Step 1: Extract min and replace root
Remove 2 (root). Move last element 7 to root: [7, 5, 3, 10]
Step 2: Bubble down 7 to restore heap
Compare 7 with children 5 and 3. Smallest child is 3. Swap 7 and 3: [3, 5, 7, 10]. Now 7 has children 10 only, which is larger, so stop.
Final Answer:
[3, 5, 7, 10] -> Option B
Quick Check:
Extract min + bubble down = [3,5,7,10] [OK]
Hint: Replace root with last, then swap down with smaller child [OK]
Common Mistakes:
Not moving last element to root before bubble down
Swapping with wrong child (larger instead of smaller)
Stopping bubble down too early
4. Consider this bubble down code snippet for a max-heap in C#:
void BubbleDown(int[] heap, int i, int size) {
int left = 2 * i + 1;
int right = 2 * i + 2;
int largest = i;
if (left < size && heap[left] > heap[largest]) largest = left;
if (right < size && heap[right] > heap[largest]) largest = right;
if (largest != i) {
int temp = heap[i]; heap[i] = heap[largest]; heap[largest] = temp;
BubbleDown(heap, largest, size);
}
}
What is the bug if the heap array is empty (size = 0) and we call BubbleDown(heap, 0, 0)?
medium
A. Infinite recursion occurs
B. Heap property is violated after call
C. No bug; function exits immediately
D. Index out of range error when accessing heap[left]
Solution
Step 1: Analyze execution with size=0
i=0, left=1, right=2. if(1 < 0 && ...) false due to short-circuit, no access to heap[1] or heap[0]. Similarly for right.
Step 2: Confirm safe exit
largest=0 == i, no swap or recursion. Function exits immediately without any array access.
Final Answer:
No bug; function exits immediately -> Option C
Quick Check:
Short-circuit prevents access on empty heap [OK]
Hint: Rely on short-circuit evaluation for bounds safety [OK]
Common Mistakes:
Misunderstanding C# short-circuit evaluation
Assuming heap[largest] is always accessed
Overlooking that no array access occurs
5. You have a max-heap represented as an array: [50, 30, 40, 10, 20, 35]. After extracting the maximum element and performing bubble down, what is the resulting heap array?
hard
A. [40, 30, 35, 10, 20]
B. [40, 30, 35, 20, 10]
C. [35, 30, 40, 10, 20]
D. [30, 20, 40, 10, 35]
Solution
Step 1: Extract max and replace root
Remove 50 (root). Move last element 35 to root: [35, 30, 40, 10, 20]
Step 2: Bubble down 35 to restore max-heap
Children: 30 (idx1), 40 (idx2). Largest is 40. Swap 35 and 40: [40, 30, 35, 10, 20]. Now at idx2 (35), children idx5 and 6 >= size 5, no children. Stop.
Final Answer:
[40, 30, 35, 10, 20] -> Option A
Quick Check:
Extract max + bubble down = [40,30,35,10,20] [OK]
Hint: Swap with largest child until heap property restored [OK]
Common Mistakes:
Swapping with smaller child in max-heap
Incorrectly identifying children during bubble down
Not moving last element to root before bubble down
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.