Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert an element into the max heap.
DSA Typescript
function insert(heap: number[], value: number) {
heap.push(value);
let index = heap.length - 1;
while (index > 0 && heap[index] [1] heap[Math.floor((index - 1) / 2)]) {
[heap[index], heap[Math.floor((index - 1) / 2)]] = [heap[Math.floor((index - 1) / 2)], heap[index]];
index = Math.floor((index - 1) / 2);
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causes min heap behavior.
Not swapping when child is greater breaks heap property.
✗ Incorrect
We swap the current element with its parent if it is greater, to maintain max heap property.
2fill in blank
mediumComplete the code to heapify down after removing the max element.
DSA Typescript
function heapifyDown(heap: number[], index: number) {
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] [1] heap[largest]) {
largest = right;
}
if (largest !== index) {
[heap[index], heap[largest]] = [heap[largest], heap[index]];
heapifyDown(heap, largest);
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' causes incorrect heap structure.
Not checking both children leads to partial heap.
✗ Incorrect
We compare children with the current largest to find the max child to swap with.
3fill in blank
hardFix the error in the code to extract the max element from the heap.
DSA Typescript
function extractMax(heap: number[]): number | null {
if (heap.length === 0) return null;
const max = heap[0];
heap[0] = heap[heap.length - 1];
heap.pop();
heapifyDown(heap, [1]);
return max;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using heap.length causes out-of-bound errors.
Using 1 or -1 skips the root and breaks heap property.
✗ Incorrect
Heapify down starts from the root at index 0 after replacing it with the last element.
4fill in blank
hardFill both blanks to complete the function that finds the kth largest element using a max heap.
DSA Typescript
function findKthLargest(nums: number[], k: number): number {
const heap: number[] = [];
for (const num of nums) {
insert(heap, num);
}
let result = 0;
for (let i = 0; i < k; i++) {
result = [1](heap);
}
return result;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using insert instead of extractMax does not remove elements.
Using heapifyDown or heapifyUp directly is incorrect here.
✗ Incorrect
We extract the max element k times to get the kth largest element.
5fill in blank
hardFill all three blanks to build a max heap from an array efficiently.
DSA Typescript
function buildMaxHeap(nums: number[]): number[] {
const heap = [...nums];
const start = Math.floor([1] / 2) - 1;
for (let i = start; i >= 0; i--) {
[2](heap, i);
}
return heap;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using heap.length before copying causes errors.
Using heapifyUp instead of heapifyDown breaks heap property.
✗ Incorrect
We start heapifying down from the last parent node (length/2 -1) to build the heap.