Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The min heap is initialized with the array [3, 1, 4, 1, 5] to build the heap structure.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
heap.Pop removes and returns the smallest element from the min heap.
3fill in blank
hardFix 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'
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.
✗ Incorrect
We need to pop k-1 times to reach the kth smallest element, so the loop runs from 0 to k-1.
4fill in blank
hardFill 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'
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.
✗ Incorrect
Len returns the length of the heap slice. The receiver is named h, so len(h) returns the size.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < will create a max heap.
Swapping i and j will invert the comparison.
✗ Incorrect
Less compares elements at indices i and j to maintain min heap order using < operator.