0
0
DSA Typescriptprogramming~20 mins

Binary Search Iterative Approach in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Binary Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Binary Search for Existing Element
What is the output of the following TypeScript code that uses iterative binary search to find the index of the number 7 in the sorted 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 arr = [1, 3, 5, 7, 9, 11];
console.log(binarySearch(arr, 7));
A4
B3
C-1
D2
Attempts:
2 left
💡 Hint
Remember that array indices start at 0 and the array is sorted.
Predict Output
intermediate
2:00remaining
Output of Binary Search for Non-Existing Element
What will be the output of this TypeScript code when searching for the number 8 in the sorted 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 arr = [2, 4, 6, 10, 12];
console.log(binarySearch(arr, 8));
A0
B2
C3
D-1
Attempts:
2 left
💡 Hint
If the target is not found, the function returns -1.
🧠 Conceptual
advanced
2:00remaining
Why Use Iterative Binary Search Instead of Recursive?
Which of the following is the main advantage of using an iterative binary search over a recursive binary search?
AIterative binary search uses less memory because it avoids the overhead of recursive calls.
BIterative binary search is easier to write and understand than recursive binary search.
CIterative binary search always finds the target faster than recursive binary search.
DIterative binary search can handle unsorted arrays better than recursive binary search.
Attempts:
2 left
💡 Hint
Think about how function calls use memory.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Binary Search Implementation
What is the bug in this iterative binary search code that causes it to fail for some inputs?
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;
}
AThe right boundary should be initialized to arr.length - 1, not arr.length.
BThe mid calculation should use Math.ceil instead of Math.floor.
CThe loop condition should be left < right instead of left <= right.
DThe function should return left instead of -1 when target is not found.
Attempts:
2 left
💡 Hint
Check the initial value of the right pointer and array indexing.
🚀 Application
expert
3:00remaining
Binary Search to Find First Occurrence in Sorted Array with Duplicates
Given a sorted array with duplicate elements, which iterative binary search modification correctly finds the index of the first occurrence of the target number?
DSA Typescript
function firstOccurrence(arr: number[], target: number): number {
  let left = 0;
  let right = arr.length - 1;
  let result = -1;
  while (left <= right) {
    const mid = Math.floor((left + right) / 2);
    if (arr[mid] === target) {
      result = mid;
      right = mid - 1;
    } else if (arr[mid] < target) {
      left = mid + 1;
    } else {
      right = mid - 1;
    }
  }
  return result;
}

const arr = [1, 2, 2, 2, 3, 4];
console.log(firstOccurrence(arr, 2));
A2
B3
C1
D-1
Attempts:
2 left
💡 Hint
When you find the target, keep searching to the left to find the first occurrence.