0
0
DSA Javascriptprogramming~10 mins

Kth Smallest Element Using Min Heap in DSA Javascript - 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 using JavaScript's built-in array and sort method.

DSA Javascript
const minHeap = arr => arr.sort((a, b) => a [1] b);
Drag options to blanks, or click blank then click option'
A-
B>
C+
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '-' causes descending order, not min heap.
Using '<' returns a boolean, leading to incorrect sorting.
2fill in blank
medium

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

DSA Javascript
const extractMin = heap => heap.[1]();
Drag options to blanks, or click blank then click option'
Apush
Bpop
Cslice
Dshift
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() removes the largest element at the end, not the smallest.
Using push() adds elements, does not remove.
3fill in blank
hard

Fix the error in the code to find the kth smallest element using a min heap.

DSA Javascript
function kthSmallest(arr, k) {
  const heap = arr.slice().sort((a, b) => a - b);
  for (let i = 1; i < k; i++) {
    heap.[1]();
  }
  return heap[0];
}
Drag options to blanks, or click blank then click option'
Apop
Bpush
Cshift
Dslice
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() removes the largest element, not the smallest.
Using push() adds elements, does not remove.
4fill in blank
hard

Fill both blanks to create a min heap and extract the kth smallest element.

DSA Javascript
function kthSmallest(arr, k) {
  const heap = arr.slice().sort((a, b) => a [1] b);
  for (let i = 1; i < k; i++) {
    heap.[2]();
  }
  return heap[0];
}
Drag options to blanks, or click blank then click option'
A-
B>
Cshift
Dpop
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' sorts descending, which is max heap behavior.
Using pop() removes last element, not smallest.
5fill in blank
hard

Fill all three blanks to build a min heap, remove k-1 smallest elements, and return the kth smallest.

DSA Javascript
function kthSmallest(arr, k) {
  const heap = arr.slice().sort((a, b) => a [1] b);
  for (let i = 1; i < k; i++) {
    heap.[2]();
  }
  return heap[3];
}
Drag options to blanks, or click blank then click option'
A-
Bshift
C[0]
D[k]
Attempts:
3 left
💡 Hint
Common Mistakes
Returning heap[k] is wrong because after removing k-1 elements, kth smallest is at index 0.
Using pop() removes largest element, not smallest.