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 on Sorted Array
What is the output of the following JavaScript code that performs binary search on a sorted array to find the index of number 7?
DSA Javascript
function binarySearch(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 sortedArray = [1, 3, 5, 7, 9, 11];
console.log(binarySearch(sortedArray, 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 sorted array [1, 3, 5, 7, 9, 11]. Binary search finds it by checking the middle element and narrowing the search range.
🧠 Conceptual
intermediate1:30remaining
Why Must the Array Be Sorted for Binary Search?
Why is it necessary for the array to be sorted before performing binary search?
Attempts:
2 left
💡 Hint
Think about how binary search decides which half of the array to ignore.
✗ Incorrect
Binary search relies on the order of elements to decide whether to search the left or right half. Without sorting, this decision cannot be made correctly.
❓ Predict Output
advanced2:00remaining
Binary Search Result on Unsorted Array
What is the output of this JavaScript code that runs binary search on an unsorted array to find number 7?
DSA Javascript
function binarySearch(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 unsortedArray = [9, 1, 11, 3, 7, 5];
console.log(binarySearch(unsortedArray, 7));Attempts:
2 left
💡 Hint
Binary search assumes sorted order; what happens if the array is not sorted?
✗ Incorrect
Binary search on an unsorted array does not guarantee correct results. It may fail to find the target even if it exists.
🧠 Conceptual
advanced1:30remaining
Effect of Sorted Order on Search Efficiency
How does having a sorted array improve the efficiency of searching compared to an unsorted array?
Attempts:
2 left
💡 Hint
Think about how many elements you can ignore each step in binary search.
✗ Incorrect
Binary search on sorted arrays cuts the search space in half each step, making it much faster than checking each element one by one.
🚀 Application
expert2:30remaining
Final Position After Multiple Binary Search Steps
Given the sorted array [2, 4, 6, 8, 10, 12, 14] and target 10, after two iterations of the binary search loop, what is the value of the variable 'left'?
DSA Javascript
const arr = [2, 4, 6, 8, 10, 12, 14]; const target = 10; let left = 0; let right = arr.length - 1; // First iteration let mid = Math.floor((left + right) / 2); if (arr[mid] < target) { left = mid + 1; } else { right = mid - 1; } // Second iteration mid = Math.floor((left + right) / 2); if (arr[mid] < target) { left = mid + 1; } else { right = mid - 1; } console.log(left);
Attempts:
2 left
💡 Hint
Trace the values of left, right, and mid after each iteration carefully.
✗ Incorrect
After the first iteration, left becomes 4 (mid=3, arr[3]=8 < 10). After the second iteration, mid=5, arr[5]=12 > 10, right=4, left remains 4.