Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the middle index in binary search.
DSA Typescript
const mid = Math.floor((low + [1]) / 2);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using mid instead of high causes incorrect calculation.
Using low twice results in always the same index.
✗ Incorrect
The middle index is calculated by averaging low and high indices.
2fill in blank
mediumComplete the code to check if the middle element is the target.
DSA Typescript
if (arr[mid] === [1]) { return mid; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing arr[mid] with mid instead of target.
Using low or arr instead of target.
✗ Incorrect
We compare the middle element with the target value to find a match.
3fill in blank
hardFix the error in updating the low index when target is greater than middle element.
DSA Typescript
if (arr[mid] < target) { low = [1]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting low to mid - 1 causes infinite loop or wrong search.
Using high or low + 1 incorrectly.
✗ Incorrect
When target is greater, we move low to mid + 1 to search the right half.
4fill in blank
hardFill both blanks to update high index when target is less than middle element and to return -1 if not found.
DSA Typescript
if (arr[mid] > target) { [1] = mid - 1; } else if (low > high) { return [2]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Updating low instead of high.
Returning 0 instead of -1 when not found.
✗ Incorrect
When target is less, high moves to mid - 1. If low passes high, target is not found, so return -1.
5fill in blank
hardFill all three blanks to complete the recursive binary search function.
DSA Typescript
function binarySearch(arr: number[], target: number, low: number, high: number): number {
if (low > high) return [1];
const mid = Math.floor((low + [2]) / 2);
if (arr[mid] === target) return mid;
else if (arr[mid] < target) return binarySearch(arr, target, mid + 1, [3]);
else return binarySearch(arr, target, low, mid - 1);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 0 instead of -1 in base case.
Using low instead of high in mid calculation.
Passing wrong high value in recursive call.
✗ Incorrect
Return -1 if not found. Calculate mid using low and high. Search right half by updating low to mid + 1 and keeping high.