0
0
DSA Typescriptprogramming~10 mins

Build Heap from Array Heapify 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 last non-leaf node in the heap array.

DSA Typescript
const lastNonLeaf = Math.floor((arr.length - 2) / [1]);
Drag options to blanks, or click blank then click option'
A2
B3
C1
D4
Attempts:
3 left
💡 Hint
Common Mistakes
Using length / 2 instead of (length - 2) / 2
Using 3 instead of 2 as divisor
Confusing leaf nodes with non-leaf nodes
2fill in blank
medium

Complete the code to swap two elements in the array during heapify.

DSA Typescript
const temp = arr[i]; arr[i] = arr[[1]]; arr[[1]] = temp;
Drag options to blanks, or click blank then click option'
Aparent
Bsmallest
Clargest
Dchild
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping with 'smallest' instead of 'largest'
Swapping with 'parent' which is the current node itself
Using an undefined variable
3fill in blank
hard

Fix the error in the heapify function's recursive call to maintain heap property.

DSA Typescript
if (largest !== i) { heapify(arr, arr.length, [1]); }
Drag options to blanks, or click blank then click option'
Alargest
Bi
C0
Darr.length
Attempts:
3 left
💡 Hint
Common Mistakes
Calling heapify with 'i' which causes infinite recursion
Calling heapify with 0 which restarts from root unnecessarily
Using arr.length as index which is out of bounds
4fill in blank
hard

Fill both blanks to complete the loop that builds the heap from the array.

DSA Typescript
for (let i = [1]; i >= 0; i[2]) { heapify(arr, arr.length, i); }
Drag options to blanks, or click blank then click option'
AMath.floor(arr.length / 2) - 1
B0
C--
D++
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from 0 and incrementing instead of decrementing
Using ++ instead of -- in the loop
Starting from arr.length instead of last non-leaf node
5fill in blank
hard

Fill all three blanks to complete the heapify function header and left child calculation.

DSA Typescript
function heapify(arr: number[], n: number, [1]: number) {
  const left = 2 * [2] + [3];
  // rest of heapify code
}
Drag options to blanks, or click blank then click option'
Ai
C1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of 1 for left child offset
Using different variable names inconsistently
Confusing left child with right child index