Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of the bubble up operation when inserting a new element into a max heap?
easy
A. To move the new element up until the heap property is restored
B. To move the new element down to the bottom of the heap
C. To remove the smallest element from the heap
D. To sort all elements in ascending order

Solution

  1. Step 1: Understand heap property

    A max heap requires each parent to be greater than or equal to its children.
  2. Step 2: Purpose of bubble up

    When a new element is added at the end, bubble up moves it up to restore the max heap property.
  3. Final Answer:

    To move the new element up until the heap property is restored -> Option A
  4. Quick Check:

    Bubble up fixes heap by moving element up [OK]
Hint: Bubble up moves new element up to fix heap [OK]
Common Mistakes:
  • Thinking bubble up moves element down
  • Confusing bubble up with removing elements
  • Assuming bubble up sorts entire heap
2. Which of the following is the correct syntax to swap two elements at indices i and parentIndex in a JavaScript array heap during bubble up?
easy
A. [heap[i], heap[parentIndex]] = [heap[parentIndex], heap[i]];
B. heap.swap(i, parentIndex);
C. swap(heap[i], heap[parentIndex]);
D. heap[i], heap[parentIndex] = heap[parentIndex], heap[i];

Solution

  1. Step 1: Identify JavaScript swap syntax

    JavaScript uses array destructuring to swap: [a, b] = [b, a];
  2. Step 2: Apply to heap array

    Swapping heap[i] and heap[parentIndex] is done by [heap[i], heap[parentIndex]] = [heap[parentIndex], heap[i]];
  3. Final Answer:

    [heap[i], heap[parentIndex]] = [heap[parentIndex], heap[i]]; -> Option A
  4. Quick Check:

    Destructuring swap syntax = [heap[i], heap[parentIndex]] = [heap[parentIndex], heap[i]]; [OK]
Hint: Use array destructuring to swap in JS [OK]
Common Mistakes:
  • Using Python-style swap syntax
  • Calling undefined swap function
  • Trying to use non-existent array method
3. Given the max heap array [50, 30, 40, 10, 20], what will be the heap array after inserting 45 and performing bubble up?
medium
A. [50, 45, 40, 10, 20, 30]
B. [50, 30, 40, 10, 20, 45]
C. [45, 30, 50, 10, 20, 40]
D. [50, 30, 45, 10, 20, 40]

Solution

  1. Step 1: Insert 45 at the end

    Heap becomes [50, 30, 40, 10, 20, 45]
  2. Step 2: Bubble up 45

    Parent index of 45 (index 5) is 2 (value 40). Since 45 > 40, swap them. New heap: [50, 30, 45, 10, 20, 40]
  3. Final Answer:

    [50, 30, 45, 10, 20, 40] -> Option D
  4. Quick Check:

    Bubble up swaps 45 with 40 [OK]
Hint: Insert at end, then swap up if bigger than parent [OK]
Common Mistakes:
  • Not swapping after insertion
  • Swapping with wrong parent index
  • Assuming insertion sorts entire heap
4. Consider this bubble up code snippet for a max heap insertion:
while (i > 0 && heap[i] > heap[parentIndex]) {
  [heap[i], heap[parentIndex]] = [heap[parentIndex], heap[i]];
  i = parentIndex;
  parentIndex = Math.floor((i - 1) / 2);
}
What is the bug if parentIndex is not updated inside the loop?
medium
A. Syntax error due to missing semicolon
B. Heap property is restored correctly
C. Loop terminates prematurely after one swap because parentIndex never changes
D. The loop never runs because i is never updated

Solution

  1. Step 1: Understand loop variables

    Inside the loop, after swap i = parentIndex, but if parentIndex is not updated, i now equals the unchanged parentIndex.
  2. Step 2: Consequence of not updating parentIndex

    The condition heap[i] > heap[parentIndex] becomes heap[pos] > heap[pos] which is false, causing premature loop termination without full restoration.
  3. Final Answer:

    Loop terminates prematurely after one swap because parentIndex never changes -> Option C
  4. Quick Check:

    Without parentIndex update, i == parentIndex stops loop early [OK]
Hint: Always update parentIndex after swapping [OK]
Common Mistakes:
  • Forgetting to update parentIndex
  • Assuming i update is enough
  • Ignoring loop condition effects
5. You have a min heap represented as [5, 10, 15, 20, 25]. You want to insert 12 and maintain the min heap property using bubble up. Which of the following sequences correctly shows the heap array after each bubble up swap?
medium
A. [5, 10, 15, 20, 25, 12] -> [5, 10, 15, 20, 25, 12]
B. [5, 10, 15, 20, 25, 12] -> [5, 10, 12, 20, 25, 15]
C. [5, 10, 15, 20, 25, 12] -> [5, 12, 15, 20, 25, 10]
D. [5, 10, 15, 20, 25, 12] -> [12, 10, 15, 20, 25, 5]

Solution

  1. Step 1: Insert 12 at the end

    Heap becomes [5, 10, 15, 20, 25, 12]
  2. Step 2: Bubble up 12

    Parent index of 12 (index 5) is 2 (value 15). Since 12 < 15, swap them. New heap: [5, 10, 12, 20, 25, 15]
  3. Final Answer:

    [5, 10, 15, 20, 25, 12] -> [5, 10, 12, 20, 25, 15] -> Option B
  4. Quick Check:

    Bubble up swaps 12 with 15 in min heap [OK]
Hint: In min heap, bubble up swaps if child < parent [OK]
Common Mistakes:
  • Swapping with wrong parent
  • Not swapping when child is smaller
  • Confusing min heap with max heap rules
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard