Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert a new element into the heap slice.
DSA Go
heap = append(heap, [1]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Appending the index or length instead of the value.
Trying to append capacity which is unrelated.
✗ Incorrect
We append the new element's value to the heap slice before reordering.
2fill in blank
mediumComplete the code to get the minimum element from a min-heap stored in a slice.
DSA Go
minElement := heap[[1]] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using last index instead of first.
Using middle index which is a leaf, not root.
✗ Incorrect
The minimum element in a min-heap is always at index 0.
3fill in blank
hardFix the error in the code that tries to remove the minimum element from a min-heap slice.
DSA Go
heap[0] = heap[len(heap)[1]] heap = heap[:len(heap)[2]]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using +1 causes index out of range error.
Using -2 removes too many elements.
✗ Incorrect
We replace root with last element at index len(heap)-1 and then slice to remove last element.
4fill in blank
hardFill both blanks to complete the code that checks if a sorted array can efficiently support insertion and deletion.
DSA Go
if insertCost == [1] && deleteCost == [2] { fmt.Println("Sorted array is inefficient for these operations") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Assuming insertion or deletion is O(1) in sorted arrays.
Confusing search cost with insertion/deletion cost.
✗ Incorrect
Insertion and deletion in a sorted array require shifting elements, costing O(n) time.
5fill in blank
hardFill all three blanks to complete the code that compares heap and sorted array for efficient minimum extraction and insertion.
DSA Go
if heapExtractMin == [1] && heapInsert == [2] && sortedArrayInsert == [3] { fmt.Println("Heap is better for dynamic data") }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up heap insertion and extraction costs.
Assuming sorted array insertion is faster than heap insertion.
✗ Incorrect
Heap extracts min in O(log n), inserts in O(log n), while sorted array inserts in O(n).