Challenge - 5 Problems
Binary Search Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of iterative binary search on sorted array
What is the output of the following C code that performs iterative binary search for the value 7 in the array?
DSA C
int binarySearch(int arr[], int n, int x) { int low = 0, high = n - 1; while (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == x) return mid; else if (arr[mid] < x) low = mid + 1; else high = mid - 1; } return -1; } int main() { int arr[] = {1, 3, 5, 7, 9, 11}; int n = sizeof(arr) / sizeof(arr[0]); int result = binarySearch(arr, n, 7); printf("%d", result); return 0; }
Attempts:
2 left
💡 Hint
Remember array indexing starts at 0. Find the position of 7 in the array.
✗ Incorrect
The array is [1, 3, 5, 7, 9, 11]. The value 7 is at index 3 (0-based). The binary search returns the index where the value is found.
🧠 Conceptual
intermediateNumber of iterations in iterative binary search
If you perform iterative binary search on a sorted array of 16 elements, what is the maximum number of iterations the while loop will run?
Attempts:
2 left
💡 Hint
Each iteration halves the search space. Think about powers of 2.
✗ Incorrect
Binary search halves the search space each iteration. For 16 elements, max iterations = log2(16) = 4.
🔧 Debug
advancedIdentify the error in this iterative binary search code
What error will this code produce when searching for 11 in the array?
DSA C
int binarySearch(int arr[], int n, int x) { int low = 0, high = n; while (low <= high) { int mid = (low + high) / 2; if (arr[mid] == x) return mid; else if (arr[mid] < x) low = mid + 1; else high = mid - 1; } return -1; } int main() { int arr[] = {2, 4, 6, 8, 10}; int n = sizeof(arr) / sizeof(arr[0]); int result = binarySearch(arr, n, 11); printf("%d", result); return 0; }
Attempts:
2 left
💡 Hint
Check the initial value of 'high' and how it is used to access array elements.
✗ Incorrect
The variable 'high' is initialized to n, which is one past the last valid index. Accessing arr[mid] when mid equals n causes out-of-bounds access.
📝 Syntax
advancedWhat error does this iterative binary search code produce?
What error will this code cause when compiled?
DSA C
int binarySearch(int arr[], int n, int x) { int low = 0, high = n - 1; while (low <= high) { int mid = low + (high - low) / 2 if (arr[mid] == x) return mid; else if (arr[mid] < x) low = mid + 1; else high = mid - 1; } return -1; }
Attempts:
2 left
💡 Hint
Look carefully at the line calculating 'mid'.
✗ Incorrect
The line calculating 'mid' is missing a semicolon at the end, causing a syntax error during compilation.
🚀 Application
expertFinal index after iterative binary search modifications
Consider this iterative binary search variant that updates 'low' and 'high' differently. What is the final value of 'low' after the loop ends when searching for 6 in the array?
DSA C
int binarySearch(int arr[], int n, int x) { int low = 0, high = n - 1; while (low < high) { int mid = low + (high - low) / 2; if (arr[mid] < x) low = mid + 1; else high = mid; } return low; } int main() { int arr[] = {1, 3, 5, 6, 8, 10}; int n = sizeof(arr) / sizeof(arr[0]); int index = binarySearch(arr, n, 6); printf("%d", index); return 0; }
Attempts:
2 left
💡 Hint
This variant finds the lower bound index of x. Trace the loop carefully.
✗ Incorrect
The loop narrows down the search space until low == high, which is the index of the first element >= x. For x=6, that index is 3.
