Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to calculate the middle index in the binary search.
DSA C++
int mid = low + ([1] - low) / 2;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using low twice instead of high and low.
Using size instead of high.
✗ Incorrect
The middle index is calculated using the high and low indices to avoid overflow.
2fill in blank
mediumComplete the code to return the index when the target is found.
DSA C++
if (arr[mid] == target) return [1];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning low or high instead of mid.
Returning -1 which means not found.
✗ Incorrect
Return the middle index where the target is found.
3fill in blank
hardFix the error in the recursive call for searching the left half.
DSA C++
return binarySearch(arr, low, [1] - 1, target);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using low or high instead of mid.
Passing target as index.
✗ Incorrect
To search the left half, we reduce the high index to mid - 1.
4fill in blank
hardFill both blanks to complete the recursive call for searching the right half.
DSA C++
return binarySearch(arr, [1] + 1, [2], target);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping low and high.
Using target as index.
✗ Incorrect
To search the right half, start from mid + 1 to high index.
5fill in blank
hardFill all three blanks to complete the base case and recursive calls of binary search.
DSA C++
if (low > high) return [1]; int mid = low + (high - low) / 2; if (arr[mid] == target) return [2]; else if (arr[mid] > target) return binarySearch(arr, low, [3] - 1, target); else return binarySearch(arr, mid + 1, high, target);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning wrong values in base case or recursive calls.
Using low instead of mid in recursive calls.
✗ Incorrect
Return -1 if not found, return mid if found, and use mid - 1 to search left half.