0
0
DSA Javascriptprogramming~20 mins

Binary Search Iterative Approach in DSA Javascript - 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 for Existing Element
What is the output of the following JavaScript code that performs an iterative binary search for the number 7 in a sorted array?
DSA Javascript
function binarySearch(arr, target) {
  let left = 0;
  let right = arr.length - 1;
  while (left <= right) {
    let 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));
A-1
B3
C2
D4
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 is the output of the following JavaScript code that performs an iterative binary search for the number 7 in a sorted array?
DSA Javascript
function binarySearch(arr, target) {
  let left = 0;
  let right = arr.length - 1;
  while (left <= right) {
    let 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, 8, 10];
console.log(binarySearch(array, 7));
A0
B3
C2
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 always runs faster than recursive binary search in all cases.
BIterative binary search is easier to write and understand than recursive binary search.
CIterative binary search uses less memory because it avoids the overhead of recursive calls.
DIterative binary search can handle unsorted arrays without sorting them first.
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 output of the following JavaScript code when searching for 5 in the array?
DSA Javascript
function binarySearch(arr, target) {
  let left = 0;
  let right = arr.length;
  while (left <= right) {
    let 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];
console.log(binarySearch(array, 5));
AReturns 2 (correct index)
BReturns -1 (target not found)
CSyntaxError: Unexpected token
DTypeError: Cannot read property 'undefined' of undefined
Attempts:
2 left
💡 Hint
Check the initial value of the right pointer and array indexing.
🚀 Application
expert
2:00remaining
Number of Iterations in Binary Search
Given a sorted array of length 31, what is the maximum number of iterations the iterative binary search algorithm will perform to find a target or conclude it is not present?
A5
B4
C6
D31
Attempts:
2 left
💡 Hint
Binary search halves the search space each iteration. Think about powers of 2.