Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

Binary Search Iterative Approach in DSA Typescript - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the middle index in the binary search loop.

DSA Typescript
let mid = Math.floor((low + [1]) / 2);
Drag options to blanks, or click blank then click option'
Ahigh
Blow
Cmid
Darray.length
Attempts:
3 left
💡 Hint
Common Mistakes
Using mid instead of high causes incorrect calculation.
Using array.length instead of high leads to wrong index.
2fill in blank
medium

Complete the code to update the low index when the target is greater than the middle element.

DSA Typescript
if (target > arr[mid]) {
  low = [1];
}
Drag options to blanks, or click blank then click option'
Amid + 1
Bhigh
Clow + 1
Dmid
Attempts:
3 left
💡 Hint
Common Mistakes
Setting low to mid causes infinite loop.
Setting low to high skips elements.
3fill in blank
hard

Fix the error in the loop condition to correctly continue searching while low is less than or equal to high.

DSA Typescript
while (low [1] high) {
  // binary search steps
}
Drag options to blanks, or click blank then click option'
A>
B!=
C<=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using < misses checking when low equals high.
Using != can cause infinite loops.
4fill in blank
hard

Fill both blanks to correctly update the high index when the target is less than the middle element.

DSA Typescript
if (target < arr[mid]) {
  high = [1];
  low = [2];
}
Drag options to blanks, or click blank then click option'
Amid - 1
Bmid + 1
Clow
Dhigh
Attempts:
3 left
💡 Hint
Common Mistakes
Setting low to mid + 1 skips elements.
Setting high to low causes errors.
5fill in blank
hard

Fill all three blanks to complete the binary search function that returns the index of the target or -1 if not found.

DSA Typescript
function binarySearch(arr: number[], target: number): number {
  let low = 0;
  let high = arr.length - 1;
  while (low [1] high) {
    let mid = Math.floor((low + high) / 2);
    if (arr[mid] === target) {
      return [2];
    } else if (target < arr[mid]) {
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }
  return [3];
}
Drag options to blanks, or click blank then click option'
A<=
Bmid
C-1
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using < in loop misses last element.
Returning low or high instead of mid is wrong.
Returning 0 instead of -1 when not found.

Practice

(1/5)
1. What is the main purpose of the Binary Search Iterative Approach in a sorted array?
easy
A. To find the maximum value in the array
B. To sort the array elements in ascending order
C. To reverse the array elements
D. To find the position of a target value efficiently by repeatedly dividing the search interval in half

Solution

  1. Step 1: Understand the goal of binary search

    Binary search is used to find a target value in a sorted array by dividing the search space repeatedly.
  2. Step 2: Identify the iterative approach

    The iterative approach uses a loop to narrow down the search range until the target is found or the range is empty.
  3. Final Answer:

    To find the position of a target value efficiently by repeatedly dividing the search interval in half -> Option D
  4. Quick Check:

    Binary search = efficient target search [OK]
Hint: Binary search halves the search space each step [OK]
Common Mistakes:
  • Confusing binary search with sorting algorithms
  • Thinking binary search works on unsorted arrays
  • Assuming it finds max/min values
2. Which of the following is the correct way to initialize the middle index in an iterative binary search in TypeScript?
easy
A. const mid = (low + high) / 2;
B. const mid = Math.floor((low + high) / 2);
C. const mid = Math.ceil((low + high) / 2);
D. const mid = low + high;

Solution

  1. Step 1: Understand index calculation in binary search

    The middle index must be an integer index, so we use Math.floor to round down the division result.
  2. Step 2: Check each option

    const mid = (low + high) / 2; returns a float, const mid = Math.floor((low + high) / 2); correctly floors the value, const mid = Math.ceil((low + high) / 2); rounds up which can cause errors, const mid = low + high; sums indices without division.
  3. Final Answer:

    const mid = Math.floor((low + high) / 2); -> Option B
  4. Quick Check:

    Use Math.floor for middle index [OK]
Hint: Always floor the average of low and high [OK]
Common Mistakes:
  • Using division without flooring causing float index
  • Using ceil which may skip elements
  • Adding indices without dividing
3. What will be the output of the following TypeScript code snippet?
function binarySearch(arr: number[], target: number): number {
  let low = 0;
  let high = arr.length - 1;
  while (low <= high) {
    const mid = Math.floor((low + high) / 2);
    if (arr[mid] === target) return mid;
    else if (arr[mid] < target) low = mid + 1;
    else high = mid - 1;
  }
  return -1;
}

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

Solution

  1. Step 1: Trace the binary search for target 6

    Initial low=0, high=4, mid=2 (element 6). arr[mid] equals target, so return 2.
  2. Step 2: Confirm output

    Function returns index 2, which is printed.
  3. Final Answer:

    2 -> Option A
  4. Quick Check:

    Target 6 found at index 2 [OK]
Hint: Check mid index value matches target first [OK]
Common Mistakes:
  • Off-by-one errors in index calculation
  • Returning wrong index when target found
  • Confusing return value with element value
4. Identify the bug in this iterative binary search code snippet and select the correct fix:
function binarySearch(arr: number[], target: number): number {
  let low = 0;
  let high = arr.length;
  while (low <= high) {
    const mid = Math.floor((low + high) / 2);
    if (arr[mid] === target) return mid;
    else if (arr[mid] < target) low = mid + 1;
    else high = mid - 1;
  }
  return -1;
}
medium
A. Change let high = arr.length; to let high = arr.length - 1;
B. Change low <= high to low < high
C. Change mid = Math.floor((low + high) / 2); to mid = Math.ceil((low + high) / 2);
D. Remove the else before if (arr[mid] < target)

Solution

  1. Step 1: Identify the initialization error

    High should be the last valid index, which is arr.length - 1, not arr.length.
  2. Step 2: Understand impact of wrong high value

    Setting high to arr.length causes out-of-bound access at arr[mid] when mid equals arr.length.
  3. Final Answer:

    Change let high = arr.length; to let high = arr.length - 1; -> Option A
  4. Quick Check:

    High index must be last valid array index [OK]
Hint: High index = array length minus one [OK]
Common Mistakes:
  • Setting high to array length causing out-of-bounds
  • Changing loop condition incorrectly
  • Using ceil instead of floor for mid
5. Given a sorted array with possible duplicate values, how can you modify the iterative binary search to find the first occurrence of a target value?
hard
A. When arr[mid] equals target, update low to mid + 1 and continue searching right
B. Return mid immediately when arr[mid] equals target
C. When arr[mid] equals target, update high to mid - 1 and continue searching left
D. Use recursion instead of iteration

Solution

  1. Step 1: Understand the problem with duplicates

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

    When target found at mid, move high to mid - 1 to continue searching left side for earlier occurrence.
  3. Step 3: Keep track of potential answer

    Store mid as potential answer and continue until low > high.
  4. Final Answer:

    When arr[mid] equals target, update high to mid - 1 and continue searching left -> Option C
  5. Quick Check:

    Move left on match to find first occurrence [OK]
Hint: On match, search left side to find first occurrence [OK]
Common Mistakes:
  • Returning immediately on first match
  • Searching right side on match
  • Ignoring duplicates in binary search
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