0
0
DSA Goprogramming~10 mins

Min Heap vs Max Heap When to Use Which in DSA Go - Interactive Comparison 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 by choosing the correct comparison operator.

DSA Go
func (h MinHeap) Less(i, j int) bool {
    return h[i] [1] h[j]
}
Drag options to blanks, or click blank then click option'
A>
B!=
C==
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < causes a max heap behavior.
Using == or != does not define order.
2fill in blank
medium

Complete the code to create a max heap by choosing the correct comparison operator.

DSA Go
func (h MaxHeap) Less(i, j int) bool {
    return h[i] [1] h[j]
}
Drag options to blanks, or click blank then click option'
A>
B==
C<
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > causes a min heap behavior.
Using == or != does not define order.
3fill in blank
hard

Fix the error in the code to pop the root element from a min heap.

DSA Go
func (h *MinHeap) Pop() interface{} {
    old := *h
    n := len(old)
    x := old[[1]]
    *h = old[0 : n-1]
    return x
}
Drag options to blanks, or click blank then click option'
An-1
B0
Cn
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using n or n-1 causes out of range or wrong element removal.
Using 1 removes the second element, not the root.
4fill in blank
hard

Fill both blanks to build a min heap from a slice of integers.

DSA Go
h := MinHeap[1]
heap.Init(&h)
return h[2]
Drag options to blanks, or click blank then click option'
A{data}
Bdata
C&h
D*h
Attempts:
3 left
💡 Hint
Common Mistakes
Using &h instead of data causes type mismatch.
Returning &h instead of h returns a pointer, not the struct.
5fill in blank
hard

Fill all three blanks to push a new value into a max heap and maintain heap property.

DSA Go
func (h *MaxHeap) Push(x interface{}) {
    *h = append(*h, [1])
    heap.[2](h)
}

func (h *MaxHeap) Pop() interface{} {
    old := *h
    n := len(old)
    x := old[[3]]
    *h = old[0 : n-1]
    return x
}
Drag options to blanks, or click blank then click option'
Ax
BFix
C0
DPush
Attempts:
3 left
💡 Hint
Common Mistakes
Appending wrong variable or calling heap.Fix instead of heap.Push.
Popping element at wrong index.