Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
In Go, to initialize an empty slice for the heap, use []int{}.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase 'push' causes compilation errors.
Using 'Add' or 'Insert' are not valid heap package methods.
✗ Incorrect
The heap package in Go uses 'Push' with uppercase P to add elements.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase 'pop' causes compilation errors.
Using 'Remove' or 'Extract' are not valid heap package methods.
✗ Incorrect
The heap package uses 'Pop' with uppercase P to remove the smallest element.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Remove' or 'Insert' instead of 'Pop' and 'Push'.
Mixing up the order of operations.
✗ Incorrect
Use 'Pop' to remove from maxHeap and 'Push' to add to minHeap.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Pop' removes elements and changes the heaps incorrectly.
Not accessing the top elements properly.
✗ Incorrect
Use 'Peek' (a custom method) to look at the top element without removing it for median calculation.