0
0
DSA Javascriptprogramming~20 mins

Binary Search Recursive Approach in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Binary Search Recursive Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Recursive Binary Search Call
What is the output of the following JavaScript code that uses recursive binary search to find the index of number 7 in the array?
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, left, mid - 1);
  else return binarySearch(arr, target, mid + 1, right);
}

const array = [1, 3, 5, 7, 9, 11];
const result = binarySearch(array, 7, 0, array.length - 1);
console.log(result);
A3
B2
C-1
D4
Attempts:
2 left
💡 Hint
Remember that array indexing starts at 0 and the function returns the index where the target is found.
Predict Output
intermediate
2:00remaining
Result of Searching Missing Element
What will be the output when searching for number 8 in the sorted array using the recursive binary search below?
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, left, mid - 1);
  else return binarySearch(arr, target, mid + 1, right);
}

const array = [2, 4, 6, 10, 12];
const result = binarySearch(array, 8, 0, array.length - 1);
console.log(result);
A2
B0
C3
D-1
Attempts:
2 left
💡 Hint
If the target is not found, the function returns -1.
🔧 Debug
advanced
2:00remaining
Identify the Error in Recursive Binary Search
What error will the following recursive binary search code produce when searching for 7 in the array?
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, left, mid - 1);
  else return binarySearch(arr, target, mid + 1, right);
}

const array = [1, 3, 5, 7];
const result = binarySearch(array, 7, 0, array.length - 1);
console.log(result);
ARangeError: Maximum call stack size exceeded
B-1
C2
DTypeError: arr is undefined
Attempts:
2 left
💡 Hint
Check the base case condition carefully and how it affects the search.
Predict Output
advanced
2:00remaining
Output of Recursive Binary Search with Duplicate Elements
What index will the recursive binary search return when searching for 3 in the array with duplicates?
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, left, mid - 1);
  else return binarySearch(arr, target, mid + 1, right);
}

const array = [1, 2, 3, 3, 3, 4, 5];
const result = binarySearch(array, 3, 0, array.length - 1);
console.log(result);
A4
B-1
C3
D2
Attempts:
2 left
💡 Hint
Binary search returns the first found index, not necessarily the first occurrence.
🧠 Conceptual
expert
2:00remaining
Why Recursive Binary Search Uses Mid Calculation with Floor?
Why does the recursive binary search calculate the middle index using Math.floor((left + right) / 2) instead of just (left + right) / 2?
ABecause array indices must be integers and floor ensures the middle index is an integer
BBecause floor rounds up to the nearest integer to avoid missing elements
CBecause floor converts the index to a string for comparison
DBecause floor is faster than other rounding methods
Attempts:
2 left
💡 Hint
Array positions cannot be fractional numbers.