Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to sort the stall positions.
DSA Go
sort.[1](stalls) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using sort.Strings on integer slice causes compile error.
Trying to sort without importing sort package.
✗ Incorrect
We use sort.Ints to sort a slice of integers representing stall positions.
2fill in blank
mediumComplete the code to check if cows can be placed with given minimum distance.
DSA Go
if stalls[i]-lastPos >= [1] {
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using maxDist instead of minDist causes wrong logic.
Comparing with midDist which is not defined here.
✗ Incorrect
We check if the distance between current stall and last placed cow is at least minDist.
3fill in blank
hardFix the error in the binary search loop condition.
DSA Go
for low <= [1] {
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using low < mid causes infinite loop.
Using high < low reverses the condition.
✗ Incorrect
The binary search continues while low is less than or equal to high.
4fill in blank
hardFill both blanks to update the binary search bounds correctly.
DSA Go
if canPlaceCows(stalls, cows, [1]) { result = [2] low = mid + 1 } else { high = mid - 1 }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Updating result to low or high instead of mid.
Incorrectly updating low or high bounds.
✗ Incorrect
If cows can be placed at distance mid, update result to mid and search higher by setting low = mid + 1.
5fill in blank
hardFill all three blanks to complete the canPlaceCows function logic.
DSA Go
count := 1 lastPos := stalls[0] for i := 1; i < len(stalls); i++ { if stalls[i]-lastPos >= [1] { count++ lastPos = stalls[i] if count == [2] { return [3] } } } return false
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning false instead of true when all cows placed.
Using wrong variable names for count or distance.
✗ Incorrect
We place cows if distance is at least minDist, count cows placed, and return true if all cows are placed.