What is the main idea behind the divide and conquer approach in binary search?
Divide the sorted list into two halves, compare the middle element with the target, and then recursively search only the half that could contain the target.
Click to reveal answer
beginner
In binary search, what happens if the middle element is equal to the target?
The search stops immediately because the target is found at the middle position.
Click to reveal answer
beginner
Why must the list be sorted for binary search to work?
Because binary search decides which half to search next based on comparisons, the list must be sorted to ensure the target is in the correct half.
Click to reveal answer
intermediate
What is the time complexity of binary search and why?
O(log n) because each step divides the search space in half, reducing the problem size exponentially.
Click to reveal answer
intermediate
Show the recursive call pattern of binary search on the array [2, 4, 6, 8, 10] when searching for 8.
1. Check middle element 6 (index 2), 8 > 6 → search right half [8, 10]
2. Check middle element 8 (index 3), found target → stop.
Click to reveal answer
What is the first step in binary search on a sorted array?
AFind the middle element and compare it with the target
BCheck the first element
CSort the array
DSearch the entire array linearly
✗ Incorrect
Binary search always starts by checking the middle element to decide which half to search next.
If the target is less than the middle element, which part of the array does binary search continue with?
ARight half
BIt stops searching
CWhole array again
DLeft half
✗ Incorrect
If the target is less than the middle element, binary search continues with the left half.
What is the base case for the recursive binary search?
AWhen the array is sorted
BWhen the target is greater than the middle element
CWhen the array is empty or the target is found
DWhen the array has more than one element
✗ Incorrect
The recursion stops when the target is found or when the search space is empty.
Why is binary search faster than linear search on large sorted arrays?
ABecause it checks every element
BBecause it divides the search space in half each time
CBecause it sorts the array first
DBecause it uses random guesses
✗ Incorrect
Binary search reduces the search space by half each step, making it much faster than checking elements one by one.
What will binary search return if the target is not in the array?
A-1 or a value indicating not found
BThe index of the closest element
CThe middle element
DThe first element
✗ Incorrect
Binary search returns -1 or a special value to indicate the target is not found.
Explain how binary search uses divide and conquer to find a target in a sorted array.
Think about how the array is split and how the search space shrinks.
You got /4 concepts.
Describe the base cases and recursive steps in a binary search algorithm.
Focus on when the search stops and how it continues.
You got /3 concepts.
Practice
(1/5)
1. What is the main idea behind Binary Search as a divide and conquer algorithm?
easy
A. It searches every element one by one from start to end.
B. It divides the search space into two halves and eliminates one half each time.
C. It sorts the array before searching.
D. It uses recursion to add elements.
Solution
Step 1: Understand divide and conquer in binary search
Binary search splits the sorted array into two halves and decides which half to continue searching in.
Step 2: Eliminate half the search space
By comparing the middle element with the target, it discards one half, reducing the problem size quickly.
Final Answer:
It divides the search space into two halves and eliminates one half each time. -> Option B
Quick Check:
Divide and conquer = split and discard half [OK]
Hint: Binary search halves the search space each step [OK]
Common Mistakes:
Thinking binary search checks every element
Confusing binary search with sorting
Believing binary search adds elements
2. Which of the following is the correct Java method signature for a binary search function on an integer array?
easy
A. public static int binarySearch(int[] arr, int target)
B. int binarySearch(int arr[], target)
C. public int binarySearch(int[] arr, int target, int start, int end)
D. static void binarySearch(int[] arr, int target)
Solution
Step 1: Check method signature correctness
public static int binarySearch(int[] arr, int target) uses public static, returns int index, and has correct parameter types and order.
Step 2: Compare other options
int binarySearch(int arr[], target) misses access modifier and parameter type for target; public int binarySearch(int[] arr, int target, int start, int end) has extra parameters but no static; static void binarySearch(int[] arr, int target) returns void which is incorrect for search.
Final Answer:
public static int binarySearch(int[] arr, int target) -> Option A
Quick Check:
Correct signature returns int and uses static [OK]
Hint: Binary search returns index as int, not void [OK]
Common Mistakes:
Omitting static keyword
Wrong return type (void instead of int)
Missing parameter types
3. What will be the output of the following Java code snippet?
int[] arr = {2, 4, 6, 8, 10};
int target = 6;
int result = binarySearch(arr, target);
System.out.println(result);
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;
}
medium
A. 2
B. 3
C. -1
D. 0
Solution
Step 1: Identify the target and array
The array is [2,4,6,8,10], target is 6.
Step 2: Trace binary search steps
Initial left=0, right=4, mid=2; arr[2]=6 equals target, so return 2.
Final Answer:
2 -> Option A
Quick Check:
Index of 6 in array = 2 [OK]
Hint: Check middle element index for target match first [OK]
Common Mistakes:
Confusing index with value
Off-by-one errors in mid calculation
Returning wrong index when target found
4. Find the error in the following binary search code and choose the correct fix.
public static int binarySearch(int[] arr, int target) {
int left = 0, right = arr.length;
while (left <= right) {
int mid = (left + right) / 2;
if (arr[mid] == target) return mid;
else if (arr[mid] < target) left = mid + 1;
else right = mid - 1;
}
return -1;
}
medium
A. Change mid calculation to mid = left - right to fix mid index.
B. Change while condition to left < right to fix infinite loop.
C. Change right to arr.length - 1 to avoid ArrayIndexOutOfBoundsException.
D. Remove else if and use only if statements.
Solution
Step 1: Identify array bounds usage
Right is set to arr.length, which is out of array index range (max index is length-1).
Step 2: Fix right boundary
Set right = arr.length - 1 to prevent accessing invalid index and avoid runtime error.
Final Answer:
Change right to arr.length - 1 to avoid ArrayIndexOutOfBoundsException. -> Option C
Quick Check:
Array indices go from 0 to length-1 [OK]
Hint: Array last index is length minus one [OK]
Common Mistakes:
Setting right to arr.length causes index error
Incorrect mid calculation
Changing loop condition incorrectly
5. You have a sorted array with duplicate elements: int[] arr = {1, 2, 2, 2, 3, 4}. How would you modify binary search to find the first occurrence of the target 2?
hard
A. Modify mid calculation to always pick left middle element.
B. Return the first found index immediately without further checks.
C. Use linear search instead of binary search for duplicates.
D. After finding target, continue searching left half to find earlier occurrence.
Solution
Step 1: Understand duplicate handling in binary search
Standard binary search returns any matching index, not necessarily first occurrence.
Step 2: Modify search to find first occurrence
When target found, move right pointer to mid-1 to continue searching left side for earlier matches.
Final Answer:
After finding target, continue searching left half to find earlier occurrence. -> Option D
Quick Check:
Find first occurrence by searching left after match [OK]
Hint: On match, search left half to find first duplicate [OK]
Common Mistakes:
Stopping at first found index
Using linear search unnecessarily
Changing mid calculation without reason
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.