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 Call
What is the output of the following Java code that performs a recursive binary search on 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; 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[] data = {2, 4, 6, 8, 10, 12, 14}; System.out.println(binarySearch(data, 0, data.length - 1, 10)); } }
Attempts:
2 left
💡 Hint
Remember array indices start at 0. Find the index where 10 is located.
✗ Incorrect
The array is sorted. The element 10 is at index 4 (0-based). The binary search returns this index.
🧠 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 problem size each step.
✗ Incorrect
Binary search splits the array into two halves and only continues searching the half where the target could be, ignoring the other half.
❓ Predict Output
advancedOutput of Iterative Binary Search with Missing Element
What is the output of this iterative binary search code when searching for 7 in the array?
DSA Java
public class BinarySearchIterative { public static int binarySearch(int[] arr, int target) { int left = 0, right = arr.length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == target) return mid; if (arr[mid] < target) left = mid + 1; else right = mid - 1; } return -1; } public static void main(String[] args) { int[] data = {1, 3, 5, 9, 11}; System.out.println(binarySearch(data, 7)); } }
Attempts:
2 left
💡 Hint
7 is not in the array. What does the code return when the element is missing?
✗ Incorrect
Since 7 is not present, the search ends with left > right and returns -1 indicating not found.
🔧 Debug
advancedIdentify the Error in Recursive Binary Search
What error will this recursive binary search code produce when searching for 5 in the array?
DSA Java
public class BinarySearchBug { 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; 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[] data = {1, 2, 3, 4, 5}; System.out.println(binarySearch(data, 0, data.length - 1, 5)); } }
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 incorrectly.
🧠 Conceptual
expertTime Complexity of Binary Search Divide and Conquer
What is the time complexity of binary search and why does the divide and conquer approach achieve this?
Attempts:
2 left
💡 Hint
Think about how many times you can halve the array before it becomes size 1.
✗ Incorrect
Binary search halves the search space each step, so the number of steps grows logarithmically with input size.
