What is the main idea behind the binary search algorithm?
Binary search works by repeatedly dividing a sorted list in half to find a target value quickly. It compares the target with the middle element and decides which half to search next.
Click to reveal answer
beginner
In the recursive binary search, what are the base cases?
The base cases are when the search range is empty (start index > end index), meaning the target is not found, or when the middle element equals the target, meaning the target is found.
Click to reveal answer
beginner
Why must the input array be sorted for binary search to work?
Binary search relies on the order of elements to decide which half to search next. If the array is not sorted, the algorithm cannot correctly eliminate half of the elements each time.
Click to reveal answer
intermediate
What is the time complexity of binary search and why?
The time complexity is O(log n) because each step cuts the search space in half, so the number of steps grows logarithmically with the size of the input.
Click to reveal answer
intermediate
Show the recursive call structure for binary search on array [2,4,6,8,10] searching for 8.
1. Check middle element (6 at index 2), 8 > 6, search right half [8,10]. 2. Check middle element (8 at index 3), found target. Recursive calls reduce search space until target is found.
Click to reveal answer
What should happen if the start index becomes greater than the end index in recursive binary search?
AReturn the end index
BContinue searching the entire array
CReturn the start index
DReturn -1 indicating target not found
✗ Incorrect
When start > end, it means the search space is empty and the target is not present.
Which of these is NOT a requirement for binary search to work correctly?
AArray must have unique elements
BArray must be sorted
CAccess to middle element in constant time
DTarget value to search
✗ Incorrect
Binary search works with duplicates as well; uniqueness is not required.
What is the middle index calculation to avoid overflow in binary search?
If the middle element is less than the target, what part of the array does recursive binary search explore next?
ALeft half
BNo further search
CRight half
DEntire array again
✗ Incorrect
If middle element < target, search continues in the right half.
What is the worst-case time complexity of recursive binary search?
AO(n)
BO(log n)
CO(n log n)
DO(1)
✗ Incorrect
Binary search halves the search space each time, leading to O(log n) complexity.
Explain how recursive binary search works step-by-step on a sorted array.
Think about how the problem size reduces each call.
You got /5 concepts.
Describe why binary search is more efficient than linear search on sorted arrays.
Focus on how many elements are checked in each approach.
You got /4 concepts.
Practice
(1/5)
1. What is the main idea behind the binary search recursive approach?
easy
A. Use recursion to reverse the array before searching
B. Divide the array into halves and search recursively in the relevant half
C. Sort the array recursively before searching
D. Search every element one by one recursively
Solution
Step 1: Understand binary search logic
Binary search works by dividing the array into two halves and deciding which half to search next based on the middle element.
Step 2: Apply recursion in binary search
Recursion calls the same function on the half where the target might be, reducing the search space each time.
Final Answer:
Divide the array into halves and search recursively in the relevant half -> Option B
Quick Check:
Binary search = divide and conquer [OK]
Hint: Think divide and conquer, search half each time [OK]
Common Mistakes:
Searching all elements recursively instead of half
Sorting inside the search function
Not reducing search space correctly
2. Which of the following is the correct base case for a recursive binary search function in C++?
easy
A. if (start > end) return -1;
B. if (start == end) return start;
C. if (start < end) return -1;
D. if (start == 0) return 0;
Solution
Step 1: Identify when search should stop
The search should stop when the start index is greater than the end index, meaning the element is not found.
Step 2: Confirm base case condition
Checking if start > end returns -1 to indicate the target is missing.
Final Answer:
if (start > end) return -1; -> Option A
Quick Check:
Base case = start > end means not found [OK]
Hint: Stop when start passes end, return -1 [OK]
Common Mistakes:
Using start == end as base case incorrectly
Returning wrong index when not found
Confusing start and end conditions
3. What will be the output of the following C++ code?
int binarySearch(int arr[], int start, int end, int target) {
if (start > end) return -1;
int mid = start + (end - start) / 2;
if (arr[mid] == target) return mid;
else if (arr[mid] > target) return binarySearch(arr, start, mid - 1, target);
else return binarySearch(arr, mid + 1, end, target);
}
int main() {
int arr[] = {2, 4, 6, 8, 10};
int index = binarySearch(arr, 0, 4, 6);
std::cout << index;
return 0;
}
medium
A. 2
B. 1
C. 3
D. -1
Solution
Step 1: Identify the target and array
The array is {2,4,6,8,10} and target is 6.
Step 2: Trace recursive calls
First mid = 2 (index 2), arr[2] = 6 matches target, so return 2 immediately.
Final Answer:
2 -> Option A
Quick Check:
Target 6 found at index 2 [OK]
Hint: Check middle element first, return index if match [OK]
Common Mistakes:
Confusing index with value
Off-by-one errors in mid calculation
Not returning correct index
4. Find the error in this recursive binary search function:
int binarySearch(int arr[], int start, int end, int target) {
if (start >= end) return -1;
int mid = (start + end) / 2;
if (arr[mid] == target) return mid;
else if (arr[mid] > target) return binarySearch(arr, start, mid - 1, target);
else return binarySearch(arr, mid + 1, end, target);
}
medium
A. Mid calculation should be start - end instead of start + end
B. Return value should be target, not index
C. Base case condition should be start > end, not start >= end
D. Recursive calls should swap start and end
Solution
Step 1: Analyze base case condition
The base case incorrectly uses start >= end, which misses checking when start == end.
Step 2: Correct base case for full search
It should be start > end to allow checking the last element at start == end.
Final Answer:
Base case condition should be start > end, not start >= end -> Option C
Quick Check:
Base case = start > end to stop [OK]
Hint: Base case must allow start == end check [OK]
Common Mistakes:
Using >= instead of > in base case
Wrong mid calculation formula
Returning wrong values
5. Given a sorted array with possible duplicate elements, how can you modify the recursive binary search to find the first occurrence of a target value?
hard
A. Use linear search after binary search to find first occurrence
B. Return mid immediately when arr[mid] == target
C. Search right half only when arr[mid] == target
D. When arr[mid] == target, continue searching left half to find earlier occurrence
Solution
Step 1: Understand duplicates in sorted array
Duplicates mean target can appear multiple times; we want the first index.
Step 2: Modify recursive search on match
When arr[mid] == target, recursively search left half to check for earlier occurrence, else return mid.
Step 3: Implement logic
If left recursive call returns valid index, return it; otherwise return mid.
Final Answer:
When arr[mid] == target, continue searching left half to find earlier occurrence -> Option D
Quick Check:
Find first occurrence = search left on match [OK]
Hint: On match, search left half to find first occurrence [OK]
Common Mistakes:
Returning first found index without checking left side
Searching right half on match
Using linear search unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.