Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the start index for binary search.
DSA Java
int start = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Starting from 1 instead of 0 causes skipping the first element.
Using array.length causes index out of bounds.
✗ Incorrect
The start index in binary search always begins at 0, the first element of the array.
2fill in blank
mediumComplete the code to calculate the middle index safely to avoid overflow.
DSA Java
int mid = start + (end [1] start) / 2;
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' causes overflow for large indexes.
Using '*' or '/' here is incorrect for midpoint calculation.
✗ Incorrect
To avoid overflow, subtract start from end before dividing by 2, then add start back.
3fill in blank
hardFix the error in the loop condition to correctly run binary search.
DSA Java
while (start [1] end) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' skips checking when start equals end.
Using '>' or '>=' causes the loop to never run or run incorrectly.
✗ Incorrect
The loop should run while start is less than or equal to end to include all elements.
4fill in blank
hardFill both blanks to update the search range after comparing mid value.
DSA Java
if (arr[mid] < target) { start = mid [1] 1; } else { end = mid [2] 1; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' when increasing start causes infinite loop.
Using '+' when decreasing end causes wrong search range.
✗ Incorrect
If mid value is less than target, move start to mid + 1; else move end to mid - 1.
5fill in blank
hardFill both blanks to return the correct index or -1 if not found.
DSA Java
if (arr[mid] == target) { return [1]; } // After loop return [2];
Attempts:
3 left
💡 Hint
Common Mistakes
Returning start or end instead of mid causes wrong index.
Not returning -1 causes incorrect result when target is missing.
✗ Incorrect
Return mid when target is found; return -1 if not found after loop ends.
