0
0
DSA Typescriptprogramming~20 mins

Heap Insert Operation Bubble Up in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Heap Insert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
A[20, 18, 15, 12, 9, 10]
B[20, 18, 10, 15, 9, 12]
C[20, 18, 15, 12, 9, 10, 15]
D[20, 18, 10, 12, 9, 15]
Attempts:
2 left
💡 Hint
Remember to bubble up the inserted value until the max-heap property is restored.
Predict Output
intermediate
2: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);
A[30, 20, 15, 10, 8, 5]
B[30, 20, 5, 10, 8, 15]
C[30, 20, 15, 5, 8, 10]
D[30, 20, 15, 10, 5, 8]
Attempts:
2 left
💡 Hint
If the inserted value is smaller than its parent, no swaps occur.
🔧 Debug
advanced
2: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);
AThe function should return the heap array after bubble up
BThe swap syntax is incorrect; should use a temp variable
CThe loop condition should be index >= 0 instead of index > 0
DparentIndex calculation is wrong; should be Math.floor((index - 1) / 2)
Attempts:
2 left
💡 Hint
Check how parent index is calculated for zero-based arrays.
Predict Output
advanced
2: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);
A[40, 30, 25, 10, 15, 5, 20, 25]
B[40, 30, 20, 10, 15, 5, 25]
C[40, 30, 25, 10, 15, 5, 20]
D[40, 30, 20, 10, 15, 25, 5]
Attempts:
2 left
💡 Hint
Bubble up swaps inserted value with parents until heap property is restored.
🧠 Conceptual
expert
2: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);
A2
B3
C4
D1
Attempts:
2 left
💡 Hint
Count how many times the inserted value swaps with its parent until it reaches the root or no longer bigger.