0
0
DSA Typescriptprogramming~10 mins

Heap Concept Structure and Properties 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 create an empty heap array.

DSA Typescript
const heap: number[] = [1];
Drag options to blanks, or click blank then click option'
Anull
B{}
C[]
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using {} which is an object, not an array.
Using null or 0 which are not arrays.
2fill in blank
medium

Complete the code to get the index of the left child of a node at index i.

DSA Typescript
function leftChild(i: number): number {
  return [1];
}
Drag options to blanks, or click blank then click option'
A2 * i
B2 * i + 1
Ci + 1
D2 * i - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 2 * i which is the index of the right child.
Using i + 1 which is just the next element.
3fill in blank
hard

Fix the error in the code to get the parent index of a node at index i.

DSA Typescript
function parent(i: number): number {
  return Math.floor([1]);
}
Drag options to blanks, or click blank then click option'
A(i - 1) / 2
Bi / 2
C(i + 1) / 2
Di - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using i / 2 which is incorrect for zero-based arrays.
Using i - 1 which is just the previous element.
4fill in blank
hard

Fill both blanks to check if a node at index i has a left child within heap size.

DSA Typescript
function hasLeftChild(i: number, size: number): boolean {
  return [1] < [2];
}
Drag options to blanks, or click blank then click option'
A2 * i + 1
B2 * i
Csize
Di
Attempts:
3 left
💡 Hint
Common Mistakes
Using 2 * i instead of 2 * i + 1 for left child.
Comparing with i instead of size.
5fill in blank
hard

Fill all three blanks to swap elements at indices i and j in the heap array.

DSA Typescript
function swap(heap: number[], i: number, j: number): void {
  const temp = heap[[1]];
  heap[[2]] = heap[[3]];
  heap[[3]] = temp;
}
Drag options to blanks, or click blank then click option'
Ai
Bj
Dtemp
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing indices i and j in assignments.
Using temp as an index instead of a value.