Challenge - 5 Problems
Heap Insert 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 max-heap represented as an array, what is the array after inserting the value 15 and performing bubble up?
DSA Typescript
const heap = [20, 18, 10, 12, 9]; function insert(heap: number[], value: number): number[] { 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; } return heap; } const result = insert(heap, 15); console.log(result);
Attempts:
2 left
💡 Hint
Remember to bubble up the inserted value until the max-heap property is restored.
✗ Incorrect
The inserted value 15 is added at the end. It swaps with 10 because 15 > 10, resulting in [20, 18, 15, 12, 9, 10]. No further swaps needed.
❓ Predict Output
intermediate2:00remaining
What is the heap array after inserting 5?
Given a max-heap array, what is the array after inserting 5 and performing bubble up?
DSA Typescript
const heap = [30, 20, 15, 10, 8]; function insert(heap: number[], value: number[]): number[] { 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; } return heap; } const result = insert(heap, 5); console.log(result);
Attempts:
2 left
💡 Hint
If the inserted value is smaller than its parent, no swaps occur.
✗ Incorrect
5 is added at the end and is smaller than its parent 15, so no bubble up happens. The heap remains [30, 20, 15, 10, 8, 5].
🔧 Debug
advanced2:00remaining
Why does this bubble up code cause an infinite loop?
Identify the bug causing an infinite loop in this bubble up implementation for a max-heap insert.
DSA Typescript
function bubbleUp(heap: number[], index: number): void {
while (index > 0) {
let parentIndex = Math.floor(index / 2);
if (heap[parentIndex] >= heap[index]) break;
[heap[parentIndex], heap[index]] = [heap[index], heap[parentIndex]];
index = parentIndex;
}
}
const heap = [50, 30, 40, 10, 20];
bubbleUp(heap, 4);
console.log(heap);Attempts:
2 left
💡 Hint
Check how parent index is calculated for zero-based arrays.
✗ Incorrect
Using Math.floor(index / 2) causes wrong parent index calculation, leading to infinite loop because index may not decrease properly. Correct formula is Math.floor((index - 1) / 2).
❓ Predict Output
advanced2:00remaining
What is the heap array after inserting 25?
Given a max-heap array, what is the array after inserting 25 and performing bubble up?
DSA Typescript
const heap = [40, 30, 20, 10, 15, 5]; function insert(heap: number[], value: number): number[] { 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; } return heap; } const result = insert(heap, 25); console.log(result);
Attempts:
2 left
💡 Hint
Bubble up swaps inserted value with parents until heap property is restored.
✗ Incorrect
25 is inserted at the end, swaps with 20 because 25 > 20, resulting in [40, 30, 25, 10, 15, 5, 20]. No further swaps needed.
🧠 Conceptual
expert2:00remaining
How many swaps occur when inserting 100 into this max-heap?
Given the max-heap array [90, 70, 80, 40, 50, 60, 30], how many swaps happen when inserting 100 and performing bubble up?
DSA Typescript
const heap = [90, 70, 80, 40, 50, 60, 30]; function insert(heap: number[], value: number): number[] { 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 heap; } const result = insert(heap, 100); console.log(result);
Attempts:
2 left
💡 Hint
Count how many times the inserted value swaps with its parent until it reaches the root or no longer bigger.
✗ Incorrect
100 swaps with 30 (1), then 80 (2), then 90 (3) and stops at root. Total swaps = 3.