0
0
DSA Typescriptprogramming~10 mins

Kth Smallest Element Using Min 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 create a min heap from the given array.

DSA Typescript
const minHeap = new MinHeap();
for (const num of nums) {
  minHeap.[1](num);
}
Drag options to blanks, or click blank then click option'
Apush
Badd
Cinsert
Dappend
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'push' or 'append' which are array methods, not heap methods.
2fill in blank
medium

Complete the code to extract the smallest element from the min heap.

DSA Typescript
const smallest = minHeap.[1]();
Drag options to blanks, or click blank then click option'
AextractMin
Bpop
Cremove
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pop' which removes the last element in an array, not the smallest in a heap.
3fill in blank
hard

Fix the error in the loop that extracts k smallest elements from the min heap.

DSA Typescript
for (let i = 0; i < k; i++) {
  result.push(minHeap.[1]());
}
Drag options to blanks, or click blank then click option'
Apop
BextractMin
Cpeek
Dremove
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'peek' which does not remove elements, causing an infinite loop or repeated values.
4fill in blank
hard

Fill both blanks to complete the function that returns the kth smallest element using a min heap.

DSA Typescript
function kthSmallest(nums: number[], k: number): number {
  const minHeap = new MinHeap();
  for (const num of nums) {
    minHeap.[1](num);
  }
  let result = 0;
  for (let i = 0; i < k; i++) {
    result = minHeap.[2]();
  }
  return result;
}
Drag options to blanks, or click blank then click option'
Ainsert
BextractMin
Cpop
Dadd
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pop' instead of 'extractMin' which does not guarantee smallest element removal.
5fill in blank
hard

Fill both blanks to complete the function that returns the kth smallest element using a min heap with early stopping.

DSA Typescript
function kthSmallest(nums: number[], k: number): number {
  const minHeap = new MinHeap();
  for (const num of nums) {
    minHeap.[1](num);
  }
  let result = 0;
  for (let i = 0; i < k; i++) {
    result = minHeap.[2]();
  }
  return result;
}
Drag options to blanks, or click blank then click option'
Ainsert
BextractMin
Cpop
Dadd
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pop' which removes the last element, not the smallest.