0
0
DSA Javascriptprogramming~10 mins

Heap Insert Operation Bubble Up in DSA Javascript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to calculate the parent index in a heap.

DSA Javascript
const parentIndex = Math.floor((index [1] 1) / 2);
Drag options to blanks, or click blank then click option'
A+
B*
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' causes wrong parent calculation.
Using '*' or '/' without subtracting 1 leads to incorrect index.
2fill in blank
medium

Complete the condition to check if bubble up should continue.

DSA Javascript
while (index > 0 && heap[index] [1] heap[parentIndex]) {
Drag options to blanks, or click blank then click option'
A>
B<=
C===
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' reverses the heap property check.
Using '===' causes infinite loop or no swaps.
3fill in blank
hard

Fix the error in swapping heap elements during bubble up.

DSA Javascript
const temp = heap[index];
heap[index] = heap[parentIndex];
heap[parentIndex] = [1];
Drag options to blanks, or click blank then click option'
Aheap[parentIndex]
Btemp
Cindex
Dheap[index]
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning heap[index] or heap[parentIndex] again causes incorrect swap.
Using index instead of temp loses the original value.
4fill in blank
hard

Fill both blanks to update index and parentIndex after swapping.

DSA Javascript
index = [1];
parentIndex = Math.floor(([2] - 1) / 2);
Drag options to blanks, or click blank then click option'
AparentIndex
Bindex
CparentIndex + 1
Dindex + 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using index instead of parentIndex causes infinite loop.
Incorrect recalculation of parentIndex breaks heap property.
5fill in blank
hard

Fill all three blanks to complete the bubble up function for heap insert.

DSA Javascript
function bubbleUp(heap) {
  let index = heap.length - 1;
  let parentIndex = Math.floor((index [1] 1) / 2);
  while (index > 0 && heap[index] [2] heap[parentIndex]) {
    const temp = heap[index];
    heap[index] = heap[parentIndex];
    heap[parentIndex] = [3];
    index = parentIndex;
    parentIndex = Math.floor((index - 1) / 2);
  }
}
Drag options to blanks, or click blank then click option'
A-
B>
Ctemp
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' in parent index calculation.
Using '<' instead of '>' in comparison.
Not using temp for swapping causes value loss.