0
0
DSA Typescriptprogramming~10 mins

Heap Extract Min or Max Bubble Down 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 index of the left child in a heap array.

DSA Typescript
function leftChildIndex(parentIndex: number): number {
  return 2 * parentIndex [1] 1;
}
Drag options to blanks, or click blank then click option'
A-
B+
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction instead of addition.
Using multiplication or division incorrectly.
2fill in blank
medium

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

DSA Typescript
function swap(heap: number[], i: number, j: number): void {
  const temp = heap[i];
  heap[i] = heap[[1]];
  heap[j] = temp;
}
Drag options to blanks, or click blank then click option'
Aj
Bi
C0
Dheap.length - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping with the wrong index.
Using the same index for both assignments.
3fill in blank
hard

Fix the error in the bubbleDown function to correctly update the index after swapping.

DSA Typescript
function bubbleDown(heap: number[], index: number): void {
  const length = heap.length;
  let smallest = index;
  const left = 2 * index + 1;
  const right = 2 * index + 2;

  if (left < length && heap[left] < heap[smallest]) {
    smallest = left;
  }
  if (right < length && heap[right] < heap[smallest]) {
    smallest = right;
  }
  if (smallest !== index) {
    swap(heap, index, smallest);
    index = [1];
    bubbleDown(heap, index);
  }
}
Drag options to blanks, or click blank then click option'
Aleft
Bindex
Csmallest
Dright
Attempts:
3 left
💡 Hint
Common Mistakes
Not updating the index at all.
Updating index to the old value instead of the child's index.
4fill in blank
hard

Fill both blanks to complete the extractMin function that removes the smallest element and restores the heap.

DSA Typescript
function extractMin(heap: number[]): number | null {
  if (heap.length === 0) return null;
  const min = heap[0];
  heap[0] = heap[[1]];
  heap.pop();
  bubbleDown(heap, [2]);
  return min;
}
Drag options to blanks, or click blank then click option'
Aheap.length - 1
B0
C1
Dheap.length
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong indices for replacement or bubble down.
Not popping the last element before bubbling down.
5fill in blank
hard

Fill all three blanks to complete the bubbleDown function for a max heap.

DSA Typescript
function bubbleDownMax(heap: number[], index: number): void {
  const length = heap.length;
  let largest = index;
  const left = 2 * index + 1;
  const right = 2 * index + 2;

  if (left < length && heap[left] [1] heap[largest]) {
    largest = left;
  }
  if (right < length && heap[right] [2] heap[largest]) {
    largest = right;
  }
  if (largest !== index) {
    swap(heap, index, largest);
    index = [3];
    bubbleDownMax(heap, index);
  }
}
Drag options to blanks, or click blank then click option'
A>
B<
Clargest
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' for max heap comparisons.
Not updating index correctly after swap.