0
0
DSA Typescriptprogramming~20 mins

Binary Search Recursive Approach in DSA Typescript - 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 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.