Challenge - 5 Problems
Binary Search Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Recursive Binary Search Call
What is the output of the following JavaScript code that uses recursive binary search to find the index of number 7 in the array?
DSA Javascript
function binarySearch(arr, target, left, right) {
if (left > right) return -1;
const mid = Math.floor((left + right) / 2);
if (arr[mid] === target) return mid;
else if (arr[mid] < target) return binarySearch(arr, target, mid + 1, right);
else return binarySearch(arr, target, left, mid - 1);
}
const array = [1, 3, 5, 7, 9, 11];
const index = binarySearch(array, 7, 0, array.length - 1);
console.log(index);Attempts:
2 left
💡 Hint
Remember that array indices start at 0 and binary search returns the index where the target is found.
✗ Incorrect
The number 7 is at index 3 in the array [1, 3, 5, 7, 9, 11]. The binary search correctly finds and returns this index.
🧠 Conceptual
intermediateDivide and Conquer Principle in Binary Search
Which statement best describes how binary search uses the divide and conquer approach?
Attempts:
2 left
💡 Hint
Think about how binary search reduces the search space at each step.
✗ Incorrect
Binary search divides the array into two halves and only continues searching in the half where the target could be, ignoring the other half. This is the core of divide and conquer.
❓ Predict Output
advancedOutput of Iterative Binary Search with Missing Element
What is the output of this iterative binary search code when searching for 8 in the array?
DSA Javascript
function binarySearchIterative(arr, target) {
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];
const result = binarySearchIterative(array, 8);
console.log(result);Attempts:
2 left
💡 Hint
If the target is not found, binary search returns -1.
✗ Incorrect
The number 8 is not in the array. The binary search completes without finding it and returns -1.
🔧 Debug
advancedIdentify the Error in Binary Search Implementation
What error will this recursive binary search code produce when searching for 9 in the array?
DSA Javascript
function binarySearch(arr, target, left, right) {
if (left > right) return -1;
const mid = Math.floor((left + right) / 2);
if (arr[mid] === target) return mid;
else if (arr[mid] < target) return binarySearch(arr, target, mid + 1, right);
else return binarySearch(arr, target, left, mid - 1);
}
const array = [1, 3, 5, 7, 9];
const index = binarySearch(array, 9, 0, array.length - 1);
console.log(index);Attempts:
2 left
💡 Hint
Check the base case condition carefully.
✗ Incorrect
The base case uses 'left > right' which correctly allows searching when left equals right, so the function finds the target at index 4 and returns it.
🧠 Conceptual
expertTime Complexity of Binary Search
What is the time complexity of binary search and why?
Attempts:
2 left
💡 Hint
Think about how many times you can divide the array in half before it becomes empty.
✗ Incorrect
Binary search cuts the search space in half each time, so the number of steps grows logarithmically with the size of the array, which is O(log n).
