Challenge - 5 Problems
Binary Search Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Binary Search Mid Calculation
What is the output of the following C++ code snippet that performs binary search on a sorted array to find the index of the target 7?
DSA C++
int binarySearch(int arr[], int left, int right, int target) { 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; } int main() { int arr[] = {1, 3, 5, 7, 9, 11}; int index = binarySearch(arr, 0, 5, 7); std::cout << index; return 0; }
Attempts:
2 left
💡 Hint
Remember that array indexing starts at 0 and the target 7 is in the middle of the array.
✗ Incorrect
The target 7 is at index 3 in the array. The binary search calculates mid as 3 and finds the target immediately.
🧠 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 after each comparison.
✗ 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.
❓ Predict Output
advancedOutput of Recursive Binary Search with Edge Case
What is the output of this recursive binary search code when searching for 8 in the array?
DSA C++
int recursiveBinarySearch(int arr[], int left, int right, int target) { if (left > right) return -1; int mid = left + (right - left) / 2; if (arr[mid] == target) return mid; else if (arr[mid] < target) return recursiveBinarySearch(arr, mid + 1, right, target); else return recursiveBinarySearch(arr, left, mid - 1, target); } int main() { int arr[] = {2, 4, 6, 8, 10}; int result = recursiveBinarySearch(arr, 0, 4, 8); std::cout << result; return 0; }
Attempts:
2 left
💡 Hint
Check the middle element and how the recursion narrows down the search.
✗ Incorrect
The target 8 is at index 3. The recursive calls find it correctly and return 3.
🔧 Debug
advancedIdentify the Error in Binary Search Implementation
What error will this binary search code produce when searching for 9 in the array?
DSA C++
int binarySearch(int arr[], int left, int right, int target) { while (left < right) { int mid = (left + right) / 2; if (arr[mid] == target) return mid; else if (arr[mid] < target) left = mid + 1; else right = mid - 1; } return -1; } int main() { int arr[] = {1, 3, 5, 7, 9}; int idx = binarySearch(arr, 0, 4, 9); std::cout << idx; return 0; }
Attempts:
2 left
💡 Hint
Check the loop condition and how it affects the search space.
✗ Incorrect
The loop condition 'left < right' excludes the case when left == right, so the target at index 4 is missed and -1 is returned.
🚀 Application
expertNumber of Recursive Calls in Binary Search
Given a sorted array of size 31, what is the maximum number of recursive calls made by a binary search to find a target or conclude it is not present?
Attempts:
2 left
💡 Hint
Think about how many times you can divide 31 by 2 until you reach 1.
✗ Incorrect
Binary search halves the search space each call. For 31 elements, maximum calls = ceil(log2(31) + 1) = 6.
