0
0
DSA Goprogramming~10 mins

Kth Smallest Element Using Min Heap in DSA Go - 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 Go
h := &IntHeap[1]
heap.Init(h)
Drag options to blanks, or click blank then click option'
A[]int{10, 20, 30}
B[]int{}
C[]int{3, 1, 4, 1, 5}
D[]int{5, 4, 3, 2, 1}
Attempts:
3 left
💡 Hint
Common Mistakes
Using an empty slice will create an empty heap.
Using a sorted slice descending will not create a min heap automatically.
2fill in blank
medium

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

DSA Go
smallest := heap.[1](h).(int)
Drag options to blanks, or click blank then click option'
ARemove
BPop
CPush
DFix
Attempts:
3 left
💡 Hint
Common Mistakes
Using Push instead of Pop will add an element instead of removing.
Remove requires an index, not used here.
3fill in blank
hard

Fix the error in the loop that pops k-1 smallest elements from the heap.

DSA Go
for i := 0; i < [1]-1; i++ {
    heap.Pop(h)
}
Drag options to blanks, or click blank then click option'
Ak
Blen(h)
Ccap(h)
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using len(h) will pop too many or too few elements.
Using 0 will not run the loop at all.
4fill in blank
hard

Fill both blanks to define the IntHeap type and implement the Len method.

DSA Go
type IntHeap []int

func (h IntHeap) [1]() int {
    return len([2])
}
Drag options to blanks, or click blank then click option'
ALen
Bh
CPush
DPop
Attempts:
3 left
💡 Hint
Common Mistakes
Using Push or Pop as method name is incorrect here.
Using a different receiver name than h will cause errors.
5fill in blank
hard

Fill all three blanks to implement the Less method for the min heap.

DSA Go
func (h IntHeap) [1](i, j int) bool {
    return h[[2]] [3] h[j]
}
Drag options to blanks, or click blank then click option'
ALess
Bi
C<
Dj
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < will create a max heap.
Swapping i and j will invert the comparison.