0
0
DSA Goprogramming~10 mins

Median of Data Stream Using Two Heaps 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 initialize the max heap for the lower half of numbers.

DSA Go
maxHeap := &IntHeap[1]
Drag options to blanks, or click blank then click option'
A[]int{}
B{}
Cmake([]int, 0)
Dnil
Attempts:
3 left
💡 Hint
Common Mistakes
Using nil instead of an empty slice causes runtime errors when pushing.
Using {} is invalid syntax for slice initialization.
2fill in blank
medium

Complete the code to push a number into the max heap.

DSA Go
heap.[1](maxHeap, num)
Drag options to blanks, or click blank then click option'
AInsert
Bpush
CAdd
DPush
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase 'push' causes compilation errors.
Using 'Add' or 'Insert' are not valid heap package methods.
3fill in blank
hard

Fix the error in the code to pop the smallest number from the min heap.

DSA Go
smallest := heap.[1](minHeap).(int)
Drag options to blanks, or click blank then click option'
Apop
BRemove
CPop
DExtract
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase 'pop' causes compilation errors.
Using 'Remove' or 'Extract' are not valid heap package methods.
4fill in blank
hard

Fill both blanks to balance the heaps by moving the top element from maxHeap to minHeap.

DSA Go
top := heap.[1](maxHeap).(int)
heap.[2](minHeap, top)
Drag options to blanks, or click blank then click option'
APop
BPush
CRemove
DInsert
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Remove' or 'Insert' instead of 'Pop' and 'Push'.
Mixing up the order of operations.
5fill in blank
hard

Fill all three blanks to calculate the median from the two heaps.

DSA Go
if maxHeap.Len() > minHeap.Len() {
    median := float64(maxHeap.[1]().(int))
    return median
} else {
    median := (float64(maxHeap.[2]().(int)) + float64(minHeap.[3]().(int))) / 2
    return median
}
Drag options to blanks, or click blank then click option'
APeek
BPop
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Pop' removes elements and changes the heaps incorrectly.
Not accessing the top elements properly.