Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Binary Search Recursive Approach in DSA Typescript - 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 on Sorted Array
What is the output of the following TypeScript code that uses recursive binary search to find the index of the target value 7?
DSA Typescript
function binarySearch(arr: number[], target: number, left: number, right: number): number {
  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);
A-1
B3
C2
D4
Attempts:
2 left
💡 Hint
Think about the position of 7 in the sorted array and how binary search divides the array.
❓ Predict Output
intermediate
2:00remaining
Result when target is not in the array
What does the recursive binary search function return when searching for the target value 8 in the array [2, 4, 6, 10, 12]?
DSA Typescript
function binarySearch(arr: number[], target: number, left: number, right: number): number {
  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);
A-1
B2
C3
D0
Attempts:
2 left
💡 Hint
If the target is not found, the function returns -1.
🧠 Conceptual
advanced
2:00remaining
Understanding Recursive Call Depth in Binary Search
If you perform a recursive binary search on a sorted array of 64 elements, what is the maximum number of recursive calls that can happen before the search ends?
A6
B64
C7
D8
Attempts:
2 left
💡 Hint
Binary search divides the array size by 2 each call. Think about powers of 2.
🔧 Debug
advanced
2:00remaining
Identify the error in this recursive binary search implementation
What error will this TypeScript recursive binary search code produce when searching for target 7 in array [1, 3, 5, 7]?
DSA Typescript
function binarySearch(arr: number[], target: number, left: number, right: number): number {
  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);
AReturns -1 even though target exists
BThrows a runtime error due to infinite recursion
CReturns the wrong index (off by one)
DReturns the correct index 2
Attempts:
2 left
💡 Hint
Check the base case condition carefully.
🚀 Application
expert
3:00remaining
Find the first occurrence of a target in a sorted array with duplicates using recursive binary search
Given a sorted array with duplicates, which recursive binary search modification correctly returns the index of the first occurrence of the target value 4 in [1, 2, 4, 4, 4, 5, 6]?
DSA Typescript
function firstOccurrence(arr: number[], target: number, left: number, right: number): number {
  if (left > right) return -1;
  const mid = Math.floor((left + right) / 2);
  if (arr[mid] === target) {
    if (mid === 0 || arr[mid - 1] < target) return mid;
    else return firstOccurrence(arr, target, left, mid - 1);
  } else if (arr[mid] < target) {
    return firstOccurrence(arr, target, mid + 1, right);
  } else {
    return firstOccurrence(arr, target, left, mid - 1);
  }
}

const array = [1, 2, 4, 4, 4, 5, 6];
const result = firstOccurrence(array, 4, 0, array.length - 1);
console.log(result);
A-1
B3
C4
D2
Attempts:
2 left
💡 Hint
Check if the found target is the first or if you need to search left side.

Practice

(1/5)
1. What is the main idea behind the binary search recursive approach?
easy
A. Search each element one by one from start to end
B. Divide the array into halves and search recursively in the correct half
C. Sort the array after each recursive call
D. Use a queue to store elements for searching

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 the middle element.
  2. Step 2: Apply recursion in binary search

    The recursive approach calls the same function on the half where the target might be, reducing the search space each time.
  3. Final Answer:

    Divide the array into halves and search recursively in the correct half -> Option B
  4. Quick Check:

    Binary search = divide and conquer [OK]
Hint: Remember: binary search splits array, then recurses [OK]
Common Mistakes:
  • Thinking binary search checks every element
  • Sorting array inside recursion
  • Using linear search logic
2. Which of the following is the correct TypeScript function signature for a recursive binary search?
easy
A. function binarySearch(arr: number[], target: string): boolean
B. function binarySearch(arr: string[], target: number): number
C. function binarySearch(arr: number[], target: number, left: number, right: number): number
D. function binarySearch(arr: number[], target: number): void

Solution

  1. Step 1: Check parameters needed for recursion

    Recursive binary search needs the array, target, and the current left and right indices to search between.
  2. Step 2: Verify return type

    The function returns the index of the target if found, so the return type is number.
  3. Final Answer:

    function binarySearch(arr: number[], target: number, left: number, right: number): number -> Option C
  4. Quick Check:

    Parameters + return type match recursive binary search [OK]
Hint: Recursive binary search needs left and right indices [OK]
Common Mistakes:
  • Missing left and right parameters
  • Wrong return type (void or boolean)
  • Mismatched array and target types
3. What will be the output of the following TypeScript code?
function binarySearch(arr: number[], target: number, left: number, right: number): number {
  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 nums = [2, 4, 6, 8, 10];
console.log(binarySearch(nums, 6, 0, nums.length - 1));
medium
A. 2
B. 1
C. -1
D. 3

Solution

  1. Step 1: Identify middle index and compare

    Initial call: left=0, right=4, mid=2. arr[2] = 6 matches target 6.
  2. Step 2: Return index when found

    Since arr[mid] equals target, function returns mid = 2 immediately.
  3. Final Answer:

    2 -> Option A
  4. Quick Check:

    Target found at index 2 [OK]
Hint: Check middle element first, then recurse [OK]
Common Mistakes:
  • Miscomputing mid index
  • Returning wrong index
  • Confusing array values with indices
4. Find the error in this recursive binary search function:
function binarySearch(arr: number[], target: number, left: number, right: number): number {
  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);
}
medium
A. The base case condition should be left > right, not left >= right
B. The mid calculation is incorrect
C. The function should return boolean instead of number
D. The recursive calls have wrong parameters

Solution

  1. Step 1: Analyze base case condition

    The base case currently returns -1 if left >= right, but it should allow left == right to check that element.
  2. Step 2: Correct base case to left > right

    Changing to left > right ensures the search includes the last element before stopping.
  3. Final Answer:

    The base case condition should be left > right, not left >= right -> Option A
  4. Quick Check:

    Base case must allow left == right [OK]
Hint: Base case stops when left passes right, not equals [OK]
Common Mistakes:
  • Stopping search too early with wrong base case
  • Miscomputing mid index
  • Wrong return type
5. Given a sorted array arr = [1, 3, 5, 7, 9, 11, 13], write a recursive binary search call to find the index of 7. What will be the sequence of mid indices checked?
hard
A. [3, 5]
B. [3, 1]
C. [3, 1, 2]
D. [3]

Solution

  1. Step 1: Calculate initial mid index

    left=0, right=6, mid = floor((0+6)/2) = 3. arr[3] = 7 matches target immediately.
  2. Step 2: No further recursive calls needed

    Since target found at first mid, no other mid indices are checked.
  3. Final Answer:

    [3] -> Option D
  4. Quick Check:

    Target found at first mid index 3 [OK]
Hint: First mid often finds target if array is balanced [OK]
Common Mistakes:
  • Assuming multiple recursive calls always happen
  • Miscomputing mid index
  • Confusing indices with values
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