Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

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

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

Practice

(1/5)
1. What is the main idea behind Binary Search as a divide and conquer algorithm?
easy
A. Check every element one by one until the target is found
B. Use a hash table to find the target instantly
C. Sort the list first and then search linearly
D. Repeatedly split the sorted list into halves to find the target

Solution

  1. Step 1: Understand binary search principle

    Binary search works by dividing the sorted list into two halves and deciding which half to continue searching in.
  2. Step 2: Compare target with middle element

    By comparing the target with the middle element, we eliminate half the list each time, making search efficient.
  3. Final Answer:

    Repeatedly split the sorted list into halves to find the target -> Option D
  4. Quick Check:

    Divide and conquer = split list halves [OK]
Hint: Divide sorted list in halves to find target fast [OK]
Common Mistakes:
  • Thinking binary search works on unsorted lists
  • Searching linearly instead of dividing
  • Ignoring the middle element comparison
2. Which of the following is the correct TypeScript function signature for a binary search function?
easy
A. function binarySearch(arr: number[], target: number): number
B. function binarySearch(arr: number, target: number[]): number
C. function binarySearch(arr: string[], target: string): void
D. function binarySearch(arr: number[], target: number): void

Solution

  1. Step 1: Identify input and output types

    Binary search takes an array of numbers and a target number, returning the index (number) or -1 if not found.
  2. Step 2: Match function signature

    function binarySearch(arr: number[], target: number): number matches input types (number array and number) and returns a number index, which is correct.
  3. Final Answer:

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

    Input: number[], number; Output: number [OK]
Hint: Input array and target number, output index number [OK]
Common Mistakes:
  • Swapping array and target types
  • Returning void instead of index
  • Using wrong array element types
3. What will be the output of the following TypeScript code?
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 nums = [2, 4, 6, 8, 10];
console.log(binarySearch(nums, 6));
medium
A. 2
B. 3
C. -1
D. 1

Solution

  1. Step 1: Identify array and target

    Array is [2,4,6,8,10], target is 6.
  2. Step 2: Trace binary search steps

    Middle index initially is 2 (value 6), which matches target, so function returns 2 immediately.
  3. Final Answer:

    2 -> Option A
  4. Quick Check:

    Index of 6 in array = 2 [OK]
Hint: Find middle index and compare target [OK]
Common Mistakes:
  • Confusing index with value
  • Off-by-one errors in mid calculation
  • Returning wrong index when target found
4. Identify the bug in this binary search implementation and choose the fix:
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;
}
medium
A. Change mid calculation to (left + right + 1) / 2
B. Change while condition to left < right
C. Change right initialization to arr.length - 1
D. Remove else if and use only if statements

Solution

  1. Step 1: Check right boundary initialization

    Right should be last valid index, which is arr.length - 1, not arr.length (out of bounds).
  2. Step 2: Understand impact on loop

    Using arr.length causes mid to sometimes be out of array range, causing runtime errors.
  3. Final Answer:

    Change right initialization to arr.length - 1 -> Option C
  4. Quick Check:

    Right boundary = last index [OK]
Hint: Right index must be last valid array index [OK]
Common Mistakes:
  • Setting right to arr.length causes out-of-bounds
  • Changing loop condition incorrectly
  • Miscalculating mid index
5. You have a sorted array with duplicate values: [1, 2, 2, 2, 3, 4]. How would you modify binary search to find the first occurrence of 2?
hard
A. Search the right half after finding 2
B. After finding 2, continue searching left half to find earlier occurrence
C. Return the first found index immediately
D. Use linear search instead of binary search

Solution

  1. Step 1: Understand duplicates in binary search

    Standard binary search returns any matching index, not necessarily the first occurrence.
  2. Step 2: Modify search to find first occurrence

    After finding target, continue searching left half to check if earlier occurrence exists, updating result accordingly.
  3. Final Answer:

    After finding 2, continue searching left half to find earlier occurrence -> Option B
  4. Quick Check:

    Find first occurrence by searching left side [OK]
Hint: Keep searching left after match to find first duplicate [OK]
Common Mistakes:
  • Stopping at first found index
  • Searching right half after match
  • Switching to 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