0
0
DSA Typescriptprogramming~10 mins

Binary Search Recursive Approach in DSA Typescript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A-
B+
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' causes wrong mid calculation.
Using '*' or '/' between left and right is incorrect.
2fill in blank
medium

Complete 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'
Aleft
Bright
Cmid
Dtarget
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'left' or 'right' instead of 'mid' causes wrong element check.
Using 'target' as index causes error.
3fill in blank
hard

Fix 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'
Amid + 1
Bmid - 1
Cleft
Dright + 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'mid - 1' searches the left half incorrectly.
Using 'right + 1' goes out of bounds.
4fill in blank
hard

Fill 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'
Aleft
Bright
Cmid - 1
Dmid + 1
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.
5fill in blank
hard

Fill 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'
Aleft
B+
C-
D*
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.