Challenge - 5 Problems
Binary Search Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput 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; } #include <stdio.h> 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); printf("%d", result); return 0; }
Attempts:
2 left
💡 Hint
Remember that array indexing starts at 0 and the function returns the index of the found element.
✗ Incorrect
The array is {1,3,5,7,9,11}. The value 7 is at index 3. The recursive binary search returns the index where the value is found.
❓ Predict Output
intermediateOutput when Element Not Found in Recursive Binary Search
What will be the output of this C code when searching for 8 in the given sorted array using recursive binary search?
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; } #include <stdio.h> int main() { int arr[] = {2, 4, 6, 10, 12}; int n = sizeof(arr) / sizeof(arr[0]); int result = binarySearch(arr, 0, n - 1, 8); printf("%d", result); return 0; }
Attempts:
2 left
💡 Hint
If the element is not present, the function returns -1.
✗ Incorrect
The value 8 is not in the array, so the function returns -1 indicating not found.
🔧 Debug
advancedIdentify the Error in Recursive Binary Search Implementation
What error will this recursive binary search code produce when compiled and run?
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; } #include <stdio.h> int main() { int arr[] = {1, 2, 3, 4, 5}; int n = sizeof(arr) / sizeof(arr[0]); int result = binarySearch(arr, 0, n - 1, 3); printf("%d", result); return 0; }
Attempts:
2 left
💡 Hint
Check the base condition carefully; it should allow the case when left equals right.
✗ Incorrect
The base condition uses 'right > left' instead of 'right >= left', so when left == right, recursion continues infinitely causing stack overflow.
🧠 Conceptual
advancedUnderstanding Recursive Binary Search Call Stack
In a recursive binary search on array {2, 4, 6, 8, 10, 12, 14}, searching for 10, which sequence of mid indices will be checked?
Attempts:
2 left
💡 Hint
Calculate mid as left + (right - left)/2 at each recursive call.
✗ Incorrect
Initial call mid = (0+6)/2=3; arr[3]=8 < 10, search right half (4 to 6); next mid=(4+6)/2=5; arr[5]=12 > 10, search left half (4 to 4); next mid=4; arr[4]=10 found.
🚀 Application
expertResult of Recursive Binary Search on Large Sorted Array
Given a sorted array of integers from 1 to 1000, what is the output index when searching recursively for the value 999?
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; } #include <stdio.h> int main() { int arr[1000]; for (int i = 0; i < 1000; i++) { arr[i] = i + 1; } int result = binarySearch(arr, 0, 999, 999); printf("%d", result); return 0; }
Attempts:
2 left
💡 Hint
Array indices start at 0, so value 999 is at index 998.
✗ Incorrect
The array has values 1 to 1000 at indices 0 to 999. Value 999 is at index 998.
