Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the middle index in binary search.
DSA Java
int mid = low + (high - low) [1] 2;
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication instead of division.
Adding instead of dividing.
Subtracting instead of dividing.
✗ Incorrect
The middle index is found by dividing the difference between high and low by 2, then adding low.
2fill in blank
mediumComplete the code to check if the middle element is the target.
DSA Java
if (arr[mid] [1] target) { return mid; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using inequality instead of equality.
Using greater than or less than instead of equality.
✗ Incorrect
We check if the middle element equals the target to return its index.
3fill in blank
hardFix the error in updating the high index when target is less than middle element.
DSA Java
if (target < arr[mid]) { high = [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting high to mid + 1 which searches wrong side.
Not updating high correctly.
✗ Incorrect
When target is less than middle element, we move high to mid - 1 to search left half.
4fill in blank
hardFill both blanks to update low index when target is greater than middle element.
DSA Java
if (target > arr[mid]) { low = [1]; high = [2]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting low to mid - 1 which searches wrong side.
Changing high incorrectly.
✗ Incorrect
When target is greater, low moves to mid + 1 and high stays the same to search right half.
5fill in blank
hardFill all three blanks to complete the binary search method header and return statement.
DSA Java
public static int [1](int[] arr, int target) { int low = 0, high = arr.length - 1; while (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == target) { return [2]; } else if (target < arr[mid]) { high = mid - 1; } else { low = mid + 1; } } return [3]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Wrong method name.
Returning wrong variable instead of mid.
Not returning -1 when target not found.
✗ Incorrect
The method is named binarySearch, returns mid when found, and -1 if not found.
