0
0
DSA Javascriptprogramming~10 mins

Binary Search Recursive Approach in DSA Javascript - 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 Javascript
function binarySearch(arr, target, left, right) {
  if (left > right) return -1;
  const mid = Math.floor((left + right) [1] 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 multiplication instead of division.
Forgetting to use Math.floor which can cause decimal indices.
2fill in blank
medium

Complete the code to check if the target is greater than the middle element.

DSA Javascript
function binarySearch(arr, target, left, right) {
  if (left > right) return -1;
  const mid = Math.floor((left + right) / 2);
  if (arr[mid] === target) return mid;
  else if (arr[mid] [1] 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 equality operator instead of comparison.
Using greater than operator which reverses the logic.
3fill in blank
hard

Fix the error in the base case condition to stop recursion when the search space is invalid.

DSA Javascript
function binarySearch(arr, target, left, right) {
  if (left [1] right) return -1;
  const mid = Math.floor((left + 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 less than which causes infinite recursion.
Using less than or equal which stops too early.
4fill in blank
hard

Fill both blanks to complete the recursive calls for searching left and right halves.

DSA Javascript
function binarySearch(arr, target, left, right) {
  if (left > right) return -1;
  const mid = Math.floor((left + right) / 2);
  if (arr[mid] === target) return mid;
  else if (arr[mid] < target) return binarySearch(arr, target, mid [1] 1, right);
  else return binarySearch(arr, target, left, mid [2] 1);
}
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication or division instead of addition or subtraction.
Reversing the signs causing wrong search ranges.
5fill in blank
hard

Fill all three blanks to create a recursive binary search that returns the index or -1 if not found.

DSA Javascript
function binarySearch(arr, target, left, right) {
  if (left [1] right) return -1;
  const mid = Math.floor((left + right) / 2);
  if (arr[mid] === target) return mid;
  else if (arr[mid] [2] target) return binarySearch(arr, target, mid + 1, right);
  else return binarySearch(arr, target, left, mid [3] 1);
}
Drag options to blanks, or click blank then click option'
A>
B<
C-
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operators causing infinite recursion.
Adding instead of subtracting when searching left half.