Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to sort the array of stall positions.
DSA C
qsort(stalls, n, sizeof(int), [1]);Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong function name for comparison.
Passing NULL instead of a comparison function.
✗ Incorrect
The qsort function requires a comparison function pointer. 'compare_ints' is the standard name for comparing integers.
2fill in blank
mediumComplete the code to check if cows can be placed with given minimum distance.
DSA C
if (count_cows >= [1]) return 1; else return 0;
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong variable name for total cows.
Checking count_cows > num_cows instead of >=.
✗ Incorrect
The variable 'num_cows' represents the total cows to place, so we check if count_cows is at least num_cows.
3fill in blank
hardFix the error in the binary search loop condition to avoid infinite loop.
DSA C
while (low <= [1]) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using mid or n in the loop condition instead of high.
Using < instead of <= causing missed cases.
✗ Incorrect
The binary search runs while low is less than or equal to high, which is the search space boundary.
4fill in blank
hardFill both blanks to update the binary search boundaries correctly.
DSA C
if (canPlaceCows(stalls, n, num_cows, mid)) low = [1]; else high = [2];
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping low and high updates.
Using low + 1 or high - 1 incorrectly.
✗ Incorrect
If cows can be placed at distance mid, we try for a bigger distance by setting low to mid + 1; else reduce high to mid - 1.
5fill in blank
hardFill all three blanks to complete the function that checks if cows can be placed with given distance.
DSA C
int canPlaceCows(int stalls[], int n, int num_cows, int dist) {
int count = 1, last_pos = stalls[0];
for (int i = 1; i < [1]; i++) {
if (stalls[i] - last_pos >= [2]) {
count++;
last_pos = stalls[i];
if (count == [3]) return 1;
}
}
return count >= [3];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong loop limit.
Comparing with wrong distance variable.
Checking count against wrong variable.
✗ Incorrect
We loop through all stalls (n), check if distance between current and last placed stall is at least dist, and count cows placed until num_cows is reached.
