0
0
DSA Typescriptprogramming~10 mins

Heap Insert Operation Bubble Up in DSA Typescript - Interactive Practice

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

Complete the code to get the parent index of a node in a heap.

DSA Typescript
const parentIndex = (index: number) => 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 / instead of - leads to incorrect index.
2fill in blank
medium

Complete the code to swap two elements in the heap array.

DSA Typescript
const swap = (arr: number[], i: number, j: number) => { const temp = arr[i]; arr[i] = arr[[1]]; arr[[1]] = temp; };
Drag options to blanks, or click blank then click option'
Aarr.length
Bi
C0
Dj
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping arr[i] with arr[i] does nothing.
Using 0 or arr.length as index causes errors.
3fill in blank
hard

Fix the error in the bubble up loop condition to continue while the current node is smaller than its parent.

DSA Typescript
let parent = Math.floor((index - 1) / 2);
while (index > 0 && heap[index] [1] heap[parent]) {
  swap(heap, index, parent);
  index = parent;
  parent = Math.floor((index - 1) / 2);
}
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.
Using '==' or '!=' causes infinite or no swaps.
4fill in blank
hard

Fill both blanks to complete the bubble up function that inserts a value and restores heap order.

DSA Typescript
function bubbleUp(heap: number[], value: number) {
  heap.push(value);
  let index = heap.length [1] 1;
  while (index > 0) {
    const parent = Math.floor((index [2] 1) / 2);
    if (heap[index] >= heap[parent]) break;
    swap(heap, index, parent);
    index = parent;
  }
}
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 indices.
Using * or / breaks the parent calculation.
5fill in blank
hard

Fill all three blanks to complete the bubble up logic with correct conditions and swapping.

DSA Typescript
function bubbleUp(heap: number[], value: number) {
  heap.push(value);
  let index = heap.length [1] 1;
  while (index > 0) {
    const parent = Math.floor((index [2] 1) / 2);
    if (heap[index] [3] heap[parent]) break;
    swap(heap, index, parent);
    index = parent;
  }
}
Drag options to blanks, or click blank then click option'
A-
B>=
C<
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of - for indices.
Using '<' or '!=' in break condition causes wrong loop behavior.