Challenge - 5 Problems
Heap Bubble Up Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the heap array after inserting 15?
Given a min-heap represented as an array, what is the state of the heap array after inserting the value 15 and performing the bubble up operation?
DSA Javascript
const heap = [10, 20, 30, 40, 50]; function insert(heap, value) { heap.push(value); let index = heap.length - 1; while (index > 0) { let parentIndex = Math.floor((index - 1) / 2); if (heap[parentIndex] <= heap[index]) break; [heap[parentIndex], heap[index]] = [heap[index], heap[parentIndex]]; index = parentIndex; } } insert(heap, 15); console.log(heap);
Attempts:
2 left
💡 Hint
Remember that bubble up swaps the inserted element with its parent while it is smaller.
✗ Incorrect
The inserted value 15 is added at the end. It swaps with 30 because 15 < 30, resulting in [10, 20, 15, 40, 50, 30]. No further swaps occur because 15 > 10.
❓ Predict Output
intermediate2:00remaining
What is the output after inserting 5 into the max-heap?
Given a max-heap array, what is the heap array after inserting 5 and performing bubble up?
DSA Javascript
const heap = [50, 30, 40, 10, 20]; function insert(heap, value) { heap.push(value); let index = heap.length - 1; while (index > 0) { let parentIndex = Math.floor((index - 1) / 2); if (heap[parentIndex] >= heap[index]) break; [heap[parentIndex], heap[index]] = [heap[index], heap[parentIndex]]; index = parentIndex; } } insert(heap, 5); console.log(heap);
Attempts:
2 left
💡 Hint
In a max-heap, bubble up swaps when the child is greater than the parent.
✗ Incorrect
5 is inserted at the end. Since 5 < 10 (its parent), no swaps happen. The heap remains [50, 30, 40, 10, 20, 5].
🔧 Debug
advanced2:00remaining
Identify the bug in this bubble up implementation
What error will occur when running this bubble up code for a min-heap insert?
DSA Javascript
function bubbleUp(heap) {
let index = heap.length - 1;
while (index > 0) {
let parentIndex = Math.floor((index - 1) / 2);
if (heap[parentIndex] <= heap[index]) break;
[heap[parentIndex], heap[index]] = [heap[index], heap[parentIndex]];
index = parentIndex;
}
}
const heap = [10, 20, 30];
heap.push(5);
bubbleUp(heap);
console.log(heap);Attempts:
2 left
💡 Hint
Check how parentIndex is calculated for a zero-based array.
✗ Incorrect
Parent index calculation is wrong. It should be Math.floor((index - 1) / 2). Using Math.floor(index / 2) causes parentIndex to be equal to index for index=1, leading to accessing undefined and a TypeError.
🧠 Conceptual
advanced2:00remaining
How many swaps occur when inserting 3 into this min-heap?
Given the min-heap array [4, 10, 8, 15, 20], how many swaps happen during bubble up after inserting 3?
DSA Javascript
const heap = [4, 10, 8, 15, 20]; function insert(heap, value) { heap.push(value); let index = heap.length - 1; let swaps = 0; while (index > 0) { let parentIndex = Math.floor((index - 1) / 2); if (heap[parentIndex] <= heap[index]) break; [heap[parentIndex], heap[index]] = [heap[index], heap[parentIndex]]; index = parentIndex; swaps++; } return swaps; } const result = insert(heap, 3); console.log(result);
Attempts:
2 left
💡 Hint
Trace the bubble up swaps step by step comparing with parents.
✗ Incorrect
3 is inserted at index 5. It swaps with 20 (index 4), then with 10 (index 1). Then it compares with 4 (index 0) and since 4 < 3, no swap. So total 2 swaps.
🚀 Application
expert3:00remaining
Final heap array after multiple inserts with bubble up
Starting with an empty min-heap, insert the values [20, 15, 30, 10, 40] one by one using bubble up. What is the final heap array?
DSA Javascript
function insert(heap, value) {
heap.push(value);
let index = heap.length - 1;
while (index > 0) {
let parentIndex = Math.floor((index - 1) / 2);
if (heap[parentIndex] <= heap[index]) break;
[heap[parentIndex], heap[index]] = [heap[index], heap[parentIndex]];
index = parentIndex;
}
}
const heap = [];
[20, 15, 30, 10, 40].forEach(v => insert(heap, v));
console.log(heap);Attempts:
2 left
💡 Hint
Insert values one by one and bubble up each time, tracking swaps.
✗ Incorrect
Insert 20: [20]
Insert 15: [15, 20]
Insert 30: [15, 20, 30]
Insert 10: bubble up swaps 10 with 20, then with 15 → [10, 15, 30, 20]
Insert 40: added at end, no swaps → [10, 15, 30, 20, 40]