0
0
DSA Javascriptprogramming~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Heap Bubble Up 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 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);
A[10, 15, 30, 40, 50, 20]
B[10, 20, 15, 40, 50, 30]
C[15, 10, 30, 40, 50, 20]
D[10, 20, 30, 40, 50, 15]
Attempts:
2 left
💡 Hint
Remember that bubble up swaps the inserted element with its parent while it is smaller.
Predict Output
intermediate
2: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);
A[50, 30, 40, 10, 5, 20]
B[50, 30, 40, 10, 20, 15]
C[50, 30, 40, 5, 20, 10]
D[50, 30, 40, 10, 20, 5]
Attempts:
2 left
💡 Hint
In a max-heap, bubble up swaps when the child is greater than the parent.
🔧 Debug
advanced
2: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);
ATypeError: Cannot read property of undefined
BInfinite loop causing crash
CSyntaxError due to missing semicolon
DNo error, output: [5, 10, 30, 20]
Attempts:
2 left
💡 Hint
Check how parentIndex is calculated for a zero-based array.
🧠 Conceptual
advanced
2: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);
A1
B3
C2
D0
Attempts:
2 left
💡 Hint
Trace the bubble up swaps step by step comparing with parents.
🚀 Application
expert
3: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);
A[10, 15, 30, 20, 40]
B[10, 15, 20, 30, 40]
C[10, 20, 15, 30, 40]
D[15, 10, 20, 30, 40]
Attempts:
2 left
💡 Hint
Insert values one by one and bubble up each time, tracking swaps.