Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Heap Extract Min or Max Bubble Down in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Heap Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output after extracting min from this min-heap?
Given the min-heap array representation, what is the heap array after extracting the minimum element and performing bubble down?
DSA Javascript
const heap = [2, 5, 3, 9, 7];

function extractMin(heap) {
  const min = heap[0];
  heap[0] = heap.pop();
  let i = 0;
  while (true) {
    let left = 2 * i + 1;
    let right = 2 * i + 2;
    let smallest = i;
    if (left < heap.length && heap[left] < heap[smallest]) smallest = left;
    if (right < heap.length && heap[right] < heap[smallest]) smallest = right;
    if (smallest === i) break;
    [heap[i], heap[smallest]] = [heap[smallest], heap[i]];
    i = smallest;
  }
  return heap;
}

console.log(extractMin(heap));
A[3, 5, 7, 9]
B[3, 7, 5, 9]
C[5, 3, 7, 9]
D[5, 7, 3, 9]
Attempts:
2 left
💡 Hint
Remember after removing the root, replace it with the last element and bubble down to restore heap property.
❓ Predict Output
intermediate
2:00remaining
What is the max-heap array after extracting max and bubbling down?
Given this max-heap array, what is the array after extracting the maximum element and performing bubble down?
DSA Javascript
const heap = [20, 15, 18, 8, 10, 17];

function extractMax(heap) {
  const max = heap[0];
  heap[0] = heap.pop();
  let i = 0;
  while (true) {
    let left = 2 * i + 1;
    let right = 2 * i + 2;
    let largest = i;
    if (left < heap.length && heap[left] > heap[largest]) largest = left;
    if (right < heap.length && heap[right] > heap[largest]) largest = right;
    if (largest === i) break;
    [heap[i], heap[largest]] = [heap[largest], heap[i]];
    i = largest;
  }
  return heap;
}

console.log(extractMax(heap));
A[18, 15, 10, 8, 17]
B[17, 15, 18, 8, 10]
C[18, 15, 17, 8, 10]
D[15, 10, 17, 8, 18]
Attempts:
2 left
💡 Hint
After removing the root, replace it with the last element and bubble down to maintain max-heap.
🔧 Debug
advanced
2:00remaining
Why does this bubble down function fail to restore min-heap?
This bubble down function is supposed to restore min-heap after extraction but fails on some inputs. What is the bug?
DSA Javascript
function bubbleDown(heap, i) {
  let left = 2 * i + 1;
  let right = 2 * i + 2;
  let smallest = i;
  if (left < heap.length && heap[left] < heap[smallest]) smallest = left;
  if (right < heap.length && heap[right] < heap[smallest]) smallest = right;
  if (smallest !== i) {
    [heap[i], heap[smallest]] = [heap[smallest], heap[i]];
    bubbleDown(heap, smallest);
  }
}

const heap = [1, 3, 6, 5, 9, 8];
heap[0] = heap[heap.length - 1];
bubbleDown(heap, 0);
console.log(heap);
AThe function does not return the heap after bubbling down, so changes are lost.
BThe function uses recursion but does not update indices correctly, causing infinite recursion.
CThe function does not check if heap is empty before popping, causing errors.
DThe function does not update the heap length after popping, causing out-of-bounds access.
Attempts:
2 left
💡 Hint
Check how the heap length changes after popping and how it affects index checks.
🧠 Conceptual
advanced
1:00remaining
What is the time complexity of bubble down operation in a heap?
Consider a heap with n elements. What is the time complexity of the bubble down operation after extracting the root?
AO(log n) because bubble down moves down one level at a time in the heap tree.
BO(n) because bubble down may need to check all elements in the heap array.
CO(1) because bubble down only swaps the root with one child once.
DO(n log n) because bubble down involves sorting the heap after extraction.
Attempts:
2 left
💡 Hint
Think about the height of a binary heap and how many swaps bubble down can perform.
🚀 Application
expert
3:00remaining
After multiple extractMin operations, what is the heap array?
Starting with this min-heap array, perform extractMin twice (removing min and bubbling down each time). What is the resulting heap array?
DSA Javascript
let heap = [1, 3, 6, 5, 9, 8, 15];

function extractMin(heap) {
  const min = heap[0];
  heap[0] = heap.pop();
  let i = 0;
  while (true) {
    let left = 2 * i + 1;
    let right = 2 * i + 2;
    let smallest = i;
    if (left < heap.length && heap[left] < heap[smallest]) smallest = left;
    if (right < heap.length && heap[right] < heap[smallest]) smallest = right;
    if (smallest === i) break;
    [heap[i], heap[smallest]] = [heap[smallest], heap[i]];
    i = smallest;
  }
  return heap;
}

extractMin(heap);
extractMin(heap);
console.log(heap);
A[3, 5, 6, 8, 9]
B[5, 8, 6, 15, 9]
C[6, 8, 15, 9]
D[5, 8, 15, 9, 6]
Attempts:
2 left
💡 Hint
Perform extractMin step-by-step, updating the heap array and bubbling down each time.

Practice

(1/5)
1. What is the main purpose of the bubble down operation in a min-heap after extracting the minimum element?
easy
A. To remove the largest element from the heap
B. To insert a new element at the bottom of the heap
C. To restore the heap property by moving the root element down to its correct position
D. To swap the last element with the root without any further changes

Solution

  1. Step 1: Understand extract-min in a min-heap

    Extracting the minimum removes the root, which breaks the heap property.
  2. Step 2: Role of bubble down

    Bubble down moves the new root element down to restore the min-heap property by swapping with smaller children.
  3. Final Answer:

    To restore the heap property by moving the root element down to its correct position -> Option C
  4. Quick Check:

    Bubble down fixes heap after extract-min [OK]
Hint: Bubble down fixes heap by pushing root down after extract [OK]
Common Mistakes:
  • Thinking bubble down inserts new elements
  • Confusing bubble down with bubble up
  • Assuming bubble down removes largest element
2. Which of the following is the correct syntax to swap two elements arr[i] and arr[j] in JavaScript during bubble down?
easy
A. [arr[i], arr[j]] = [arr[j], arr[i]];
B. swap(arr[i], arr[j]);
C. arr[i] = arr[j]; arr[j] = arr[i];
D. temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;

Solution

  1. Step 1: Identify JavaScript swap syntax

    ES6 allows swapping with destructuring: [a, b] = [b, a].
  2. Step 2: Check each option

    [arr[i], arr[j]] = [arr[j], arr[i]]; uses correct destructuring syntax. temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; is valid but missing var or let for temp, so incomplete. Options A and C are incorrect syntax or logic.
  3. Final Answer:

    [arr[i], arr[j]] = [arr[j], arr[i]]; -> Option A
  4. Quick Check:

    Use destructuring swap in JS [OK]
Hint: Use ES6 destructuring for swap: [a,b]=[b,a] [OK]
Common Mistakes:
  • Using undefined swap function
  • Overwriting values without temp variable
  • Incorrect assignment order causing same values
3. Given the min-heap array [2, 5, 3, 8, 10], after extracting the minimum and performing bubble down, what is the resulting heap array?
medium
A. [3, 5, 8, 10]
B. [5, 3, 8, 10]
C. [5, 8, 3, 10]
D. [3, 5, 10, 8]

Solution

  1. Step 1: Extract min and replace root

    Remove 2 (root), move last element 10 to root: [10, 5, 3, 8]
  2. Step 2: Bubble down 10

    Compare 10 with children 5 and 3. Smallest child is 3 at index 2. Swap 10 and 3: [3, 5, 10, 8]. Now 10 at index 2 has no children (indices 5 and 6 out of bounds), so stop.
  3. Final Answer:

    [3, 5, 10, 8] -> Option D
  4. Quick Check:

    Bubble down restores min-heap [OK]
Hint: Replace root with last, then swap down with smaller child [OK]
Common Mistakes:
  • Not swapping with smallest child
  • Forgetting to remove last element
  • Swapping with larger child
4. Consider this bubble down code snippet for a max-heap extract operation:
function bubbleDown(arr, index) {
  let left = 2 * index + 1;
  let right = 2 * index + 2;
  let largest = index;
  if (left < arr.length && arr[left] > arr[largest]) {
    largest = left;
  }
  if (right < arr.length && arr[right] > arr[largest]) {
    largest = right;
  }
  if (largest !== index) {
    [arr[index], arr[largest]] = [arr[largest], arr[index]];
    bubbleDown(arr, largest);
  }
}
What is the bug if the heap array is empty after extract and bubbleDown is called with index 0?
medium
A. Infinite recursion because largest never equals index
B. No bug; function handles empty array correctly
C. Accessing arr[0] causes undefined behavior on empty array
D. Swapping elements without checking array length

Solution

  1. Step 1: Analyze empty array case

    If arr is empty, arr.length = 0, left and right indices are 1 and 2, both >= length, so no swaps.
  2. Step 2: Check access at arr[index]

    No accesses occur because child indices out of bounds and && short-circuits prevent evaluating arr[largest]. Function handles empty array safely.
  3. Final Answer:

    No bug; function handles empty array correctly -> Option B
  4. Quick Check:

    Check array length before accessing index [OK]
Hint: Always check array length before accessing indices [OK]
Common Mistakes:
  • Assuming recursion stops automatically
  • Ignoring empty array edge case
  • Not validating index bounds
5. You have a max-heap represented as [50, 30, 40, 10, 20, 35]. After extracting the maximum and performing bubble down, what is the final heap array?
hard
A. [40, 30, 35, 10, 20]
B. [40, 30, 35, 20, 10]
C. [40, 35, 30, 10, 20]
D. [35, 30, 40, 10, 20]

Solution

  1. Step 1: Extract max and replace root

    Remove 50 (root), move last element 35 to root: [35, 30, 40, 10, 20]
  2. Step 2: Bubble down 35

    Compare 35 with children 30 and 40. Largest child is 40 at index 2. Swap 35 and 40: [40, 30, 35, 10, 20]. Now 35 at index 2 has children beyond array length, so stop.
  3. Final Answer:

    [40, 30, 35, 10, 20] -> Option A
  4. Quick Check:

    Bubble down swaps with largest child in max-heap [OK]
Hint: Swap root with largest child until heap property restored [OK]
Common Mistakes:
  • Swapping with smaller child
  • Not moving last element to root
  • Stopping bubble down too early
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