Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to calculate the middle index in the recursive binary search.
DSA Typescript
function binarySearch(arr: number[], target: number, left: number, right: number): number {
if (left > right) return -1;
const mid = Math.floor((left [1] right) / 2);
if (arr[mid] === target) return mid;
else if (arr[mid] < target) return binarySearch(arr, target, mid + 1, right);
else return binarySearch(arr, target, left, mid - 1);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' causes wrong mid calculation.
Using '*' or '/' between left and right is incorrect.
✗ Incorrect
The middle index is calculated by adding left and right, then dividing by 2. So the operator between left and right is '+'.
2fill in blank
mediumComplete the code to check if the target is found at the middle index.
DSA Typescript
if (arr[[1]] === target) return mid;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'left' or 'right' instead of 'mid' causes wrong element check.
Using 'target' as index causes error.
✗ Incorrect
We compare the element at the middle index 'mid' with the target to check if it's found.
3fill in blank
hardFix the error in the recursive call when the target is greater than the middle element.
DSA Typescript
else if (arr[mid] < target) return binarySearch(arr, target, [1], right);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'mid - 1' searches the left half incorrectly.
Using 'right + 1' goes out of bounds.
✗ Incorrect
When target is greater than middle element, search in the right half starting from mid + 1.
4fill in blank
hardFill both blanks to complete the base case and recursive call for the left half search.
DSA Typescript
if ([1] > right) return -1; else return binarySearch(arr, target, left, [2]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right in base case causes infinite recursion.
Using mid + 1 instead of mid - 1 searches wrong half.
✗ Incorrect
The base case checks if left is greater than right. The recursive call searches left half up to mid - 1.
5fill in blank
hardFill all three blanks to complete the recursive binary search function.
DSA Typescript
function binarySearch(arr: number[], target: number, left: number, right: number): number {
if ([1] > right) return -1;
const mid = Math.floor((left [2] right) / 2);
if (arr[mid] === target) return mid;
else if (arr[mid] < target) return binarySearch(arr, target, mid [3] 1, right);
else return binarySearch(arr, target, left, mid - 1);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' in mid calculation causes wrong index.
Using mid - 1 instead of mid + 1 for right half causes infinite recursion.
✗ Incorrect
Base case checks if left > right. Middle index is (left + right) / 2. Right half search starts at mid + 1.