Challenge - 5 Problems
Binary Search Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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));Attempts:
2 left
💡 Hint
Remember that array indices start at 0 and the array is sorted.
✗ Incorrect
The number 7 is at index 3 in the array [1, 3, 5, 7, 9, 11]. The binary search finds it and returns the index 3.
❓ Predict Output
intermediate2: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));Attempts:
2 left
💡 Hint
If the target is not found, the function returns -1.
✗ Incorrect
The number 7 is not in the array [2, 4, 6, 8, 10], so the function returns -1.
🧠 Conceptual
advanced2: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?
Attempts:
2 left
💡 Hint
Think about how function calls use memory.
✗ Incorrect
Recursive calls add overhead to the call stack, which uses more memory. Iterative binary search uses a loop and constant memory.
🔧 Debug
advanced2: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));Attempts:
2 left
💡 Hint
Check the initial value of the right pointer and array indexing.
✗ Incorrect
The code returns 2 (correct index) because the target is found at mid=2 on the first iteration. However, right = arr.length is a bug: it risks out-of-bounds access to arr[arr.length] (undefined in JS).
🚀 Application
expert2: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?
Attempts:
2 left
💡 Hint
Binary search halves the search space each iteration. Think about powers of 2.
✗ Incorrect
Binary search reduces the search space by half each iteration. For 31 elements, maximum iterations = ceil(log2(31)) = 5.