0
0
DSA Typescriptprogramming~10 mins

Kth Largest Element Using Max Heap 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 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'
A<=
B===
C<
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causes min heap behavior.
Not swapping when child is greater breaks heap property.
2fill in blank
medium

Complete 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'
A>
B<
C===
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' causes incorrect heap structure.
Not checking both children leads to partial heap.
3fill in blank
hard

Fix 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'
Aheap.length
B0
C1
D-1
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.
4fill in blank
hard

Fill 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'
AextractMax
BheapifyUp
CheapifyDown
Dinsert
Attempts:
3 left
💡 Hint
Common Mistakes
Using insert instead of extractMax does not remove elements.
Using heapifyDown or heapifyUp directly is incorrect here.
5fill in blank
hard

Fill 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'
Aheap.length
BheapifyDown
Cnums.length
DheapifyUp
Attempts:
3 left
💡 Hint
Common Mistakes
Using heap.length before copying causes errors.
Using heapifyUp instead of heapifyDown breaks heap property.