0
0
DSA Typescriptprogramming~20 mins

Binary Search as Divide and Conquer in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Binary Search Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Binary Search on Sorted Array
What is the output of the following TypeScript code that performs binary search on a sorted array to find the index of the number 7?
DSA Typescript
function binarySearch(arr: number[], target: number): number {
  let left = 0;
  let right = arr.length - 1;
  while (left <= right) {
    const mid = Math.floor((left + right) / 2);
    if (arr[mid] === target) {
      return mid;
    } else if (arr[mid] < target) {
      left = mid + 1;
    } else {
      right = mid - 1;
    }
  }
  return -1;
}

const array = [1, 3, 5, 7, 9, 11];
console.log(binarySearch(array, 7));
A2
B3
C-1
D4
Attempts:
2 left
💡 Hint
Remember that array indices start at 0 and the array is sorted.
Predict Output
intermediate
2:00remaining
Binary Search Result When Target Not Present
What will the following TypeScript binary search function return when searching for 8 in the array?
DSA Typescript
function binarySearch(arr: number[], target: number): number {
  let left = 0;
  let right = arr.length - 1;
  while (left <= right) {
    const mid = Math.floor((left + right) / 2);
    if (arr[mid] === target) {
      return mid;
    } else if (arr[mid] < target) {
      left = mid + 1;
    } else {
      right = mid - 1;
    }
  }
  return -1;
}

const array = [2, 4, 6, 10, 12];
console.log(binarySearch(array, 8));
A-1
B2
C3
D0
Attempts:
2 left
💡 Hint
If the target is not found, the function returns -1.
🔧 Debug
advanced
2:00remaining
Identify the Error in Binary Search Implementation
What error will occur when running this TypeScript binary search code?
DSA Typescript
function binarySearch(arr: number[], target: number): number {
  let left = 0;
  let right = arr.length;
  while (left <= right) {
    const mid = Math.floor((left + right) / 2);
    if (arr[mid] === target) {
      return mid;
    } else if (arr[mid] < target) {
      left = mid + 1;
    } else {
      right = mid - 1;
    }
  }
  return -1;
}

const array = [1, 2, 3, 4, 5];
console.log(binarySearch(array, 3));
ASyntaxError: Unexpected token
BReturns 2
CRangeError: Maximum call stack size exceeded
DTypeError: Cannot read property '3' of undefined
Attempts:
2 left
💡 Hint
Check the initial value of the right pointer and array indexing.
🧠 Conceptual
advanced
2:00remaining
Why Binary Search is Divide and Conquer
Which statement best explains why binary search is considered a divide and conquer algorithm?
AIt merges two sorted arrays into one sorted array.
BIt checks every element one by one until it finds the target.
CIt divides the array into two halves and recursively searches only one half where the target can be.
DIt sorts the array before searching for the target.
Attempts:
2 left
💡 Hint
Divide and conquer means breaking the problem into smaller parts and solving one part.
Predict Output
expert
3:00remaining
Output of Recursive Binary Search with Duplicate Elements
What is the output of the following TypeScript recursive binary search code when searching for 5 in the array with duplicates?
DSA Typescript
function recursiveBinarySearch(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 recursiveBinarySearch(arr, target, left, mid - 1);
    } else {
      return mid;
    }
  } else if (arr[mid] < target) {
    return recursiveBinarySearch(arr, target, mid + 1, right);
  } else {
    return recursiveBinarySearch(arr, target, left, mid - 1);
  }
}

const array = [1, 3, 5, 5, 5, 7, 9];
console.log(recursiveBinarySearch(array, 5, 0, array.length - 1));
A2
B3
C4
D-1
Attempts:
2 left
💡 Hint
The function returns the first occurrence index of the target.