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 Java code that performs a recursive binary search for the value 7 in the array?
DSA Java
public class BinarySearch { public static int binarySearch(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 binarySearch(arr, left, mid - 1, target); } else { return binarySearch(arr, mid + 1, right, target); } } public static void main(String[] args) { int[] array = {1, 3, 5, 7, 9, 11}; int result = binarySearch(array, 0, array.length - 1, 7); System.out.println(result); } }
Attempts:
2 left
💡 Hint
Remember that array indices start at 0 and the array is sorted.
✗ Incorrect
The target 7 is at index 3 in the array. The recursive binary search finds it and returns the index.
❓ Predict Output
intermediateResult when target is not in the array
What will be printed when searching for the value 8 in the same array using the recursive binary search?
DSA Java
public class BinarySearch { public static int binarySearch(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 binarySearch(arr, left, mid - 1, target); } else { return binarySearch(arr, mid + 1, right, target); } } public static void main(String[] args) { int[] array = {1, 3, 5, 7, 9, 11}; int result = binarySearch(array, 0, array.length - 1, 8); System.out.println(result); } }
Attempts:
2 left
💡 Hint
If the target is not found, the function returns -1.
✗ Incorrect
Since 8 is not in the array, the function returns -1 indicating the target is absent.
🧠 Conceptual
advancedUnderstanding Recursive Binary Search Base Case
In the recursive binary search function, what is the purpose of the condition 'if (left > right)'?
Attempts:
2 left
💡 Hint
Think about what happens when the search boundaries cross.
✗ Incorrect
When left index is greater than right, it means the search space is empty and the target is not present.
🔧 Debug
advancedIdentify the error in this recursive binary search code
What error will occur when running this recursive binary search code?
DSA Java
public class BinarySearch { public static int binarySearch(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 binarySearch(arr, left, mid - 1, target); } else { return binarySearch(arr, mid + 1, right, target); } } public static void main(String[] args) { int[] array = {2, 4, 6, 8, 10}; int result = binarySearch(array, 0, array.length - 1, 10); System.out.println(result); } }
Attempts:
2 left
💡 Hint
Check the base case condition carefully.
✗ Incorrect
The base case uses 'left >= right' which excludes the case when left == right, missing the last element check, so it returns -1 even if target is present.
🚀 Application
expertNumber of Recursive Calls for Binary Search
Given a sorted array of 64 elements, what is the maximum number of recursive calls made by a recursive binary search to find a target or conclude it is not present?
Attempts:
2 left
💡 Hint
Think about how many times you can divide 64 by 2 until you reach 1.
✗ Incorrect
Binary search divides the search space by half each call. For 64 elements, maximum calls = log2(64) + 1 = 6 + 1 = 7 calls. But since the first call counts as call 1, the maximum depth is 7 calls. However, the question asks for maximum number of recursive calls, which is 7 calls including the first. So answer is 7.
