Challenge - 5 Problems
Binary Search Recursive Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output 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, left, mid - 1);
else return binarySearch(arr, target, mid + 1, right);
}
const array = [1, 3, 5, 7, 9, 11];
const result = binarySearch(array, 7, 0, array.length - 1);
console.log(result);Attempts:
2 left
💡 Hint
Remember that array indexing starts at 0 and the function returns the index where the target is found.
✗ Incorrect
The target 7 is at index 3 in the array [1, 3, 5, 7, 9, 11]. The recursive binary search finds it and returns 3.
❓ Predict Output
intermediate2:00remaining
Result of Searching Missing Element
What will be the output when searching for number 8 in the sorted array using the recursive binary search below?
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, left, mid - 1);
else return binarySearch(arr, target, mid + 1, right);
}
const array = [2, 4, 6, 10, 12];
const result = binarySearch(array, 8, 0, array.length - 1);
console.log(result);Attempts:
2 left
💡 Hint
If the target is not found, the function returns -1.
✗ Incorrect
Since 8 is not in the array, the recursive calls eventually cross over and return -1.
🔧 Debug
advanced2:00remaining
Identify the Error in Recursive Binary Search
What error will the following recursive binary search code produce when searching for 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, left, mid - 1);
else return binarySearch(arr, target, mid + 1, right);
}
const array = [1, 3, 5, 7];
const result = binarySearch(array, 7, 0, array.length - 1);
console.log(result);Attempts:
2 left
💡 Hint
Check the base case condition carefully and how it affects the search.
✗ Incorrect
The base case uses 'left >= right' instead of 'left > right'. This causes the function to return -1 even when left equals right, missing the last element. Searching for 7 (at index 3), the recursion reaches left=3, right=3 and returns -1 without checking arr[3].
❓ Predict Output
advanced2:00remaining
Output of Recursive Binary Search with Duplicate Elements
What index will the recursive binary search return when searching for 3 in the array with duplicates?
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, left, mid - 1);
else return binarySearch(arr, target, mid + 1, right);
}
const array = [1, 2, 3, 3, 3, 4, 5];
const result = binarySearch(array, 3, 0, array.length - 1);
console.log(result);Attempts:
2 left
💡 Hint
Binary search returns the first found index, not necessarily the first occurrence.
✗ Incorrect
The recursive binary search finds mid = Math.floor((0 + 6) / 2) = 3, arr[3] === 3 and returns 3. It does not guarantee the first or last occurrence in duplicates.
🧠 Conceptual
expert2:00remaining
Why Recursive Binary Search Uses Mid Calculation with Floor?
Why does the recursive binary search calculate the middle index using Math.floor((left + right) / 2) instead of just (left + right) / 2?
Attempts:
2 left
💡 Hint
Array positions cannot be fractional numbers.
✗ Incorrect
Array indices are whole numbers. Using floor ensures the middle index is an integer so the function accesses a valid position in the array.