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 Recursive Binary Search on Sorted Array
What is the output of the following C++ code that performs a recursive binary search for the value 7 in the array?
DSA C++
int binarySearch(int arr[], int left, int right, int x) { if (right >= left) { int mid = left + (right - left) / 2; if (arr[mid] == x) return mid; if (arr[mid] > x) return binarySearch(arr, left, mid - 1, x); return binarySearch(arr, mid + 1, right, x); } return -1; } int main() { int arr[] = {1, 3, 5, 7, 9, 11}; int n = sizeof(arr) / sizeof(arr[0]); int result = binarySearch(arr, 0, n - 1, 7); std::cout << result << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Remember that array indexing starts at 0 and binary search returns the index of the found element.
✗ Incorrect
The value 7 is at index 3 in the array {1, 3, 5, 7, 9, 11}. The recursive binary search returns this index.
❓ Predict Output
intermediate2:00remaining
Result of Recursive Binary Search When Element Not Present
What will be the output of this recursive binary search code when searching for 8 in the array?
DSA C++
int binarySearch(int arr[], int left, int right, int x) { if (right >= left) { int mid = left + (right - left) / 2; if (arr[mid] == x) return mid; if (arr[mid] > x) return binarySearch(arr, left, mid - 1, x); return binarySearch(arr, mid + 1, right, x); } return -1; } int main() { int arr[] = {2, 4, 6, 10, 12}; int n = sizeof(arr) / sizeof(arr[0]); int result = binarySearch(arr, 0, n - 1, 8); std::cout << result << std::endl; return 0; }
Attempts:
2 left
💡 Hint
If the element is not found, the function returns -1.
✗ Incorrect
Since 8 is not in the array, the recursive calls eventually return -1 indicating not found.
🧠 Conceptual
advanced1:30remaining
Understanding Recursive Binary Search Base Case
In the recursive binary search function, what is the purpose of the base case condition 'if (right < left)'?
Attempts:
2 left
💡 Hint
Think about what happens when the search boundaries cross.
✗ Incorrect
When right < left, it means there are no elements left to search, so the element is not found.
❓ Predict Output
advanced2:00remaining
Output of Recursive Binary Search with Duplicate Elements
Given the array with duplicates, what index will the recursive binary search return when searching for 5?
DSA C++
int binarySearch(int arr[], int left, int right, int x) { if (right >= left) { int mid = left + (right - left) / 2; if (arr[mid] == x) return mid; if (arr[mid] > x) return binarySearch(arr, left, mid - 1, x); return binarySearch(arr, mid + 1, right, x); } return -1; } int main() { int arr[] = {1, 3, 5, 5, 5, 7, 9}; int n = sizeof(arr) / sizeof(arr[0]); int result = binarySearch(arr, 0, n - 1, 5); std::cout << result << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Binary search returns the first found index, not necessarily the first occurrence.
✗ Incorrect
The mid calculation first finds index 3 where 5 is located, so it returns 3.
🧠 Conceptual
expert1:30remaining
Time Complexity of Recursive Binary Search
What is the time complexity of the recursive binary search algorithm on a sorted array of size n?
Attempts:
2 left
💡 Hint
Each recursive call halves the search space.
✗ Incorrect
Binary search divides the array in half each time, leading to logarithmic time complexity O(log n).