Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the start index for binary search.
DSA C
int start = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Setting start to 1 skips the first element.
Setting start to -1 causes invalid index access.
✗ Incorrect
The start index in binary search should be 0 to cover the whole array from the beginning.
2fill in blank
mediumComplete the code to calculate the middle index safely.
DSA C
int mid = start + ([1] - start) / 2;
Attempts:
3 left
💡 Hint
Common Mistakes
Using (start + end) / 2 can cause overflow.
Using mid in calculation before assignment is incorrect.
✗ Incorrect
To avoid overflow, mid is calculated as start + (end - start) / 2.
3fill in blank
hardFix the error in the loop condition to continue searching while valid.
DSA C
while (start [1] end) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using only less than skips checking when start equals end.
Using greater than reverses the logic.
✗ Incorrect
The loop should run while start is less than or equal to end to cover all elements.
4fill in blank
hardFill both blanks to update the search range after checking mid value.
DSA C
if (arr[mid] < target) { start = mid [1] 1; } else { end = mid [2] 1; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication or division instead of addition or subtraction.
Not adjusting start or end correctly causes infinite loops.
✗ Incorrect
If mid value is less than target, move start to mid + 1; else move end to mid - 1.
5fill in blank
hardFill all three blanks to complete the binary search function returning index or -1.
DSA C
int binarySearch(int arr[], int n, int target) {
int start = 0, end = n [1] 1;
while (start [2] end) {
int mid = start + (end - start) / 2;
if (arr[mid] == target) {
return [3];
} else if (arr[mid] < target) {
start = mid + 1;
} else {
end = mid - 1;
}
}
return -1;
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting end to n causes out-of-bounds access.
Using < instead of <= in loop misses last element.
Returning start or end instead of mid is incorrect.
✗ Incorrect
End is n - 1, loop runs while start <= end, return mid when found.
