Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create an empty heap array.
DSA Typescript
const heap: number[] = [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using {} which is an object, not an array.
Using null or 0 which are not arrays.
✗ Incorrect
An empty heap is represented by an empty array [].
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
In a heap stored as an array, left child index = 2 * i + 1.
3fill in blank
hardFix 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'
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.
✗ Incorrect
Parent index = floor((i - 1) / 2) in a heap array.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 2 * i instead of 2 * i + 1 for left child.
Comparing with i instead of size.
✗ Incorrect
Left child index is 2 * i + 1 and must be less than heap size.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing indices i and j in assignments.
Using temp as an index instead of a value.
✗ Incorrect
Swap uses temp to hold heap[i], then assigns heap[j] to heap[i], and temp back to heap[j].