Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to sort the array of stall positions.
DSA C#
Array.[1](stalls);Attempts:
3 left
💡 Hint
Common Mistakes
Using Array.Reverse instead of Array.Sort.
Forgetting to sort the stalls before processing.
✗ Incorrect
We use Array.Sort to arrange stall positions in ascending order, which is essential for the binary search approach.
2fill in blank
mediumComplete the code to check if cows can be placed with at least 'distance' apart.
DSA C#
int count = 1; int lastPosition = stalls[0]; for (int i = 1; i < stalls.Length; i++) { if (stalls[i] - lastPosition >= [1]) { count++; lastPosition = stalls[i]; } } return count >= cows;
Attempts:
3 left
💡 Hint
Common Mistakes
Using cows instead of distance in the if condition.
Comparing with lastPosition instead of distance.
✗ Incorrect
We check if the gap between current stall and last placed cow is at least 'distance'.
3fill in blank
hardFix the error in the binary search loop condition to find the maximum minimum distance.
DSA C#
while (low [1] high) { int mid = (low + high) / 2; if (CanPlaceCows(stalls, cows, mid)) { result = mid; low = mid + 1; } else { high = mid - 1; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '<=' causing missing the last check.
Using '>' or '>=' which reverses the logic.
✗ Incorrect
We use 'low <= high' to ensure the binary search covers all possible distances including when low equals high.
4fill in blank
hardFill both blanks to initialize the binary search boundaries correctly.
DSA C#
int low = [1]; int high = [2];
Attempts:
3 left
💡 Hint
Common Mistakes
Setting low to 0 which allows zero distance.
Setting high to the last stall position instead of the difference.
✗ Incorrect
The minimum distance can start from 1, and the maximum distance is the difference between the last and first stall after sorting.
5fill in blank
hardFill all three blanks to complete the main function that finds the maximum minimum distance.
DSA C#
Array.Sort(stalls); int low = [1]; int high = [2]; int result = 0; while (low <= high) { int mid = (low + high) / 2; if ([3](stalls, cows, mid)) { result = mid; low = mid + 1; } else { high = mid - 1; } } return result;
Attempts:
3 left
💡 Hint
Common Mistakes
Using cows instead of CanPlaceCows in the if condition.
Setting low or high incorrectly.
✗ Incorrect
We sort stalls, set low to 1, high to max gap, and use CanPlaceCows to check feasibility.
