Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main idea behind the binary search recursive approach when searching for a value in a sorted array?
easy
A. Divide the array into halves and search recursively in the half where the value can be.
B. Check every element one by one from start to end.
C. Sort the array first and then search linearly.
D. Use a loop to check elements until the value is found.

Solution

  1. Step 1: Understand binary search logic

    Binary search works by dividing the sorted array into two halves and deciding which half to search next based on comparison.
  2. Step 2: Recognize recursive approach

    In recursion, the function calls itself on the half where the target value may exist, reducing the search space each time.
  3. Final Answer:

    Divide the array into halves and search recursively in the half where the value can be. -> Option A
  4. Quick Check:

    Binary search = divide and conquer [OK]
Hint: Remember: binary search splits array, recursion repeats on half [OK]
Common Mistakes:
  • Thinking binary search checks every element linearly
  • Confusing recursion with iteration
  • Not requiring the array to be sorted
2. Which of the following is the correct base case condition in a recursive binary search function in JavaScript?
easy
A. if (array.length == 0) return 0;
B. if (start > end) return -1;
C. if (start == end) return start;
D. if (mid == target) return mid + 1;

Solution

  1. Step 1: Identify base case meaning

    The base case stops recursion when the search space is empty, which happens if start index passes end index.
  2. Step 2: Match base case condition

    The condition start > end means no elements left to search, so return -1 to indicate not found.
  3. Final Answer:

    if (start > end) return -1; -> Option B
  4. Quick Check:

    Base case = empty search space [OK]
Hint: Base case ends recursion when start passes end [OK]
Common Mistakes:
  • Using incorrect comparison like start == end as base case
  • Returning wrong index or value in base case
  • Confusing base case with recursive step
3. What will be the output of the following JavaScript code?
function binarySearch(arr, target, start, end) {
  if (start > end) return -1;
  const mid = Math.floor((start + end) / 2);
  if (arr[mid] === target) return mid;
  else if (arr[mid] > target) return binarySearch(arr, target, start, mid - 1);
  else return binarySearch(arr, target, mid + 1, end);
}

const array = [2, 4, 6, 8, 10];
console.log(binarySearch(array, 6, 0, array.length - 1));
medium
A. 2
B. 3
C. -1
D. 1

Solution

  1. Step 1: Trace initial call

    Start=0, end=4, mid=2 (element 6). Since arr[mid] == target (6), return mid = 2.
  2. Step 2: Confirm output

    The function returns 2 immediately without further recursion.
  3. Final Answer:

    2 -> Option A
  4. Quick Check:

    Target found at index 2 [OK]
Hint: Check mid index value first to find target quickly [OK]
Common Mistakes:
  • Miscomputing mid index
  • Confusing index with value
  • Forgetting zero-based indexing
4. Identify the error in the following recursive binary search function and choose the correct fix.
function binarySearch(arr, target, start, end) {
  if (start >= end) return -1;
  const mid = Math.floor((start + end) / 2);
  if (arr[mid] === target) return mid;
  else if (arr[mid] > target) return binarySearch(arr, target, start, mid - 1);
  else return binarySearch(arr, target, mid + 1, end);
}
medium
A. Change mid calculation to (start + end + 1) / 2;
B. Remove else if and use only if statements.
C. Return mid + 1 when found instead of mid;
D. Change base case to if (start > end) return -1;

Solution

  1. Step 1: Analyze base case condition

    The base case incorrectly uses start >= end, which misses the case when start == end and the element could still be at that index.
  2. Step 2: Correct base case

    Changing it to start > end ensures the search includes the last element before stopping.
  3. Final Answer:

    Change base case to if (start > end) return -1; -> Option D
  4. Quick Check:

    Base case must allow start == end check [OK]
Hint: Base case should allow checking when start equals end [OK]
Common Mistakes:
  • Stopping search too early by wrong base case
  • Changing mid calculation unnecessarily
  • Returning wrong index on success
5. Given a sorted array with possible duplicate values, how can you modify the recursive binary search to find the first occurrence of a target value?
hard
A. Search right half only when arr[mid] == target.
B. Return mid immediately when arr[mid] == target without further search.
C. When arr[mid] == target, continue searching left half to find earlier occurrence.
D. Use linear search after binary search to find first occurrence.

Solution

  1. Step 1: Understand duplicates in binary search

    Standard binary search returns any occurrence, but to find first occurrence, we must check if earlier duplicates exist.
  2. Step 2: Modify recursion on match

    When arr[mid] == target, recursively search left half to find if an earlier occurrence exists; if none, mid is first occurrence.
  3. Final Answer:

    When arr[mid] == target, continue searching left half to find earlier occurrence. -> Option C
  4. Quick Check:

    Find first occurrence by searching left on match [OK]
Hint: On match, search left half to find first duplicate [OK]
Common Mistakes:
  • Returning first found index without checking left side
  • Searching right half on match incorrectly
  • Using linear search unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard