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 on Sorted Array
What is the output of the following Java code that performs an iterative binary search for the value 7 in the array?
DSA Java
public class Main { 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; } else if (arr[mid] < target) { left = mid + 1; } else { right = mid - 1; } } return -1; } public static void main(String[] args) { int[] arr = {1, 3, 5, 7, 9, 11}; System.out.println(binarySearch(arr, 7)); } }
Attempts:
2 left
💡 Hint
Remember that array indices start at 0 and the array is sorted.
✗ Incorrect
The value 7 is at index 3 in the array. The binary search finds it and returns the index.
❓ Predict Output
intermediateBinary Search Result When Target Not Present
What will be the output of this Java code when searching for 8 in the given sorted array?
DSA Java
public class Main { 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; } else if (arr[mid] < target) { left = mid + 1; } else { right = mid - 1; } } return -1; } public static void main(String[] args) { int[] arr = {2, 4, 6, 10, 12}; System.out.println(binarySearch(arr, 8)); } }
Attempts:
2 left
💡 Hint
If the target is not found, the function returns -1.
✗ Incorrect
Since 8 is not in the array, the binary search returns -1 indicating not found.
🔧 Debug
advancedIdentify the Error in Binary Search Implementation
What error will this Java code produce when running the binary search for target 11?
DSA Java
public class Main { public static int binarySearch(int[] arr, int target) { int left = 0, right = arr.length; 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; } public static void main(String[] args) { int[] arr = {1, 3, 5, 7, 9}; System.out.println(binarySearch(arr, 11)); } }
Attempts:
2 left
💡 Hint
Check the initial value of the right pointer and array indexing.
✗ Incorrect
The right pointer is set to arr.length which is out of bounds for indexing, causing ArrayIndexOutOfBoundsException.
🧠 Conceptual
advancedNumber of Iterations in Binary Search
Given a sorted array of 64 elements, what is the maximum number of iterations the iterative binary search algorithm will perform to find a target or conclude it is not present?
Attempts:
2 left
💡 Hint
Binary search halves the search space each iteration.
✗ Incorrect
Binary search takes at most log2(n) + 1 iterations. For 64 elements, log2(64) = 6, so max iterations = 7.
🚀 Application
expertFind Insert Position Using Iterative Binary Search
What is the output of this Java code that uses iterative binary search to find the insert position for target 8 in a sorted array?
DSA Java
public class Main { public static int searchInsert(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; } else if (arr[mid] < target) { left = mid + 1; } else { right = mid - 1; } } return left; } public static void main(String[] args) { int[] arr = {1, 3, 5, 6, 9}; System.out.println(searchInsert(arr, 8)); } }
Attempts:
2 left
💡 Hint
If target is not found, the function returns the index where it can be inserted to keep array sorted.
✗ Incorrect
8 is not in the array. It should be inserted at index 4 to keep the array sorted.
