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 C++ code that performs binary search on a sorted array?
DSA C++
int binarySearch(int arr[], int size, int target) { int left = 0, right = size - 1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == target) return mid; else if (arr[mid] < target) left = mid + 1; else right = mid - 1; } return -1; } #include <iostream> int main() { int arr[] = {2, 4, 6, 8, 10, 12}; int index = binarySearch(arr, 6, 8); std::cout << index << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Remember that array indexing starts at 0 and binary search returns the index of the target if found.
✗ Incorrect
The target 8 is at index 3 in the sorted array. Binary search finds it and returns the index.
🧠 Conceptual
intermediate1:30remaining
Why Must the Array Be Sorted for Binary Search?
Why is it necessary for the array to be sorted before applying binary search?
Attempts:
2 left
💡 Hint
Think about how binary search decides which half of the array to search next.
✗ Incorrect
Binary search relies on the order of elements to eliminate half of the search space each time. Without sorting, this logic fails.
❓ Predict Output
advanced2:00remaining
Result of Binary Search on Unsorted Array
What will be the output of this C++ code that runs binary search on an unsorted array?
DSA C++
int binarySearch(int arr[], int size, int target) { int left = 0, right = size - 1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == target) return mid; else if (arr[mid] < target) left = mid + 1; else right = mid - 1; } return -1; } #include <iostream> int main() { int arr[] = {10, 2, 8, 4, 12, 6}; int index = binarySearch(arr, 6, 4); std::cout << index << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Binary search assumes sorted order. What happens if the array is not sorted?
✗ Incorrect
Since the array is not sorted, binary search may not find the target correctly and returns -1 indicating not found.
🔧 Debug
advanced2:30remaining
Identify the Bug in Binary Search Implementation
What is the bug in this binary search code that causes it to fail for some inputs?
DSA C++
int binarySearch(int arr[], int size, int target) { int left = 0, right = size - 1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == target) return mid; else if (arr[mid] < target) left = mid + 1; else right = mid - 1; } return -1; }
Attempts:
2 left
💡 Hint
Check the valid index range for the array.
✗ Incorrect
Array indices go from 0 to size-1. Initializing 'right' to 'size' causes out-of-bound access.
🧠 Conceptual
expert1:30remaining
Why Binary Search is More Efficient Than Linear Search on Sorted Data
Why is binary search more efficient than linear search when the data is sorted?
Attempts:
2 left
💡 Hint
Think about how many elements are left to check after each step in binary search.
✗ Incorrect
Binary search cuts the search space in half each time, leading to O(log n) time, much faster than linear search's O(n).