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?
Because binary search decides which half to search based on comparisons, the array must be sorted to ensure that all smaller elements are on one side and larger on the other.
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, quickly reducing the number of elements to check.
Click to reveal answer
intermediate
How does the recursive call change the search range in binary search?
The recursive call updates either the start or end index to the half where the target might be, effectively narrowing the search range by half each time.
Click to reveal answer
What should happen if the middle element equals the target in recursive binary search?
AReturn the middle index
BSearch the left half
CSearch the right half
DReturn -1
✗ Incorrect
If the middle element matches the target, the search is successful and the middle index is returned.
What condition ends the recursion when the target is not found?
Amiddle element == target
Bstart index == end index
Cstart index > end index
Darray is empty
✗ Incorrect
When the start index becomes greater than the end index, it means the target is not in the array and recursion ends.
Which of these is required for binary search to work correctly?
AArray must be sorted
BArray must be unsorted
CArray must contain unique elements
DArray must be reversed
✗ Incorrect
Binary search requires the array to be sorted to decide which half to search next.
What is the time complexity of binary search?
AO(n)
BO(1)
CO(n log n)
DO(log n)
✗ Incorrect
Binary search has O(log n) time complexity because it halves the search space each step.
In recursive binary search, if the target is less than the middle element, what happens next?
ASearch the right half
BSearch the left half
CReturn the middle index
DReturn -1
✗ Incorrect
If the target is less than the middle element, the search continues in the left half.
Explain how recursive binary search works step-by-step.
Think about dividing the array and calling the function again with updated indices.
You got /5 concepts.
Why is recursion a good fit for implementing binary search?
Consider how the search space reduces and how recursion naturally models this.
You got /4 concepts.
Practice
(1/5)
1. What is the main idea behind the binary search recursive approach?
easy
A. Divide the search range in half recursively until the target is found or range is empty
B. Check every element one by one until the target is found
C. Sort the array recursively before searching
D. Use a queue to store elements and search iteratively
Solution
Step 1: Understand binary search logic
Binary search works by repeatedly dividing the search range in half to find the target efficiently.
Step 2: Recognize recursive approach
The recursive approach calls the same function on the half where the target might be, until found or range is empty.
Final Answer:
Divide the search range in half recursively until the target is found or range is empty -> Option A
Quick Check:
Binary search recursive = divide and conquer [OK]
Hint: Binary search halves range recursively to find target fast [OK]
Common Mistakes:
Thinking binary search checks every element
Confusing recursion with iteration
Assuming array must be sorted during search
2. Which of the following is the correct Rust function signature for a recursive binary search on a slice of integers?
easy
A. fn binary_search(arr: &[i32], target: i32) -> bool
D. fn binary_search(arr: &mut [i32], target: i32) -> i32
Solution
Step 1: Identify parameters for recursion
Recursive binary search needs the array slice, target, and current low and high indices to narrow search.
Step 2: Match correct signature
fn binary_search(arr: &[i32], target: i32, low: usize, high: usize) -> Option<usize> matches this with slice reference, target, low and high indices, and returns Option with found index.
A. The recursive calls should swap low and high parameters
B. Mid calculation should use low + (high - low) / 2 to avoid overflow
C. The function should return usize instead of Option<usize>
D. The base case condition should be low > high, not low >= high
Solution
Step 1: Analyze base case condition
Base case currently returns None if low >= high, but when low == high, the element at that index should still be checked.
Step 2: Correct base case to low > high
Changing condition to low > high ensures the last element is checked before returning None.
Final Answer:
The base case condition should be low > high, not low >= high -> Option D
Quick Check:
Base case must allow low == high check [OK]
Hint: Base case must allow checking when low equals high [OK]
Common Mistakes:
Stopping search too early by wrong base case
Ignoring possibility of single element left
Misusing mid calculation causing overflow
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. When target is found, continue searching left half recursively to find earlier occurrence
B. Return immediately when target is found without further search
C. Use iterative binary search instead of recursive
D. Sort the array again before searching
Solution
Step 1: Understand duplicates and first occurrence
Duplicates mean target may appear multiple times; first occurrence is the smallest index with target.
Step 2: Modify recursion to search left half after finding target
When target is found at mid, recursively search left half to check for earlier occurrence, else return mid.
Final Answer:
When target is found, continue searching left half recursively to find earlier occurrence -> Option A
Quick Check:
Find first occurrence by searching left after match [OK]
Hint: After finding target, search left half to find first occurrence [OK]
Common Mistakes:
Stopping at first found index ignoring duplicates
Sorting array again unnecessarily
Switching to iterative without reason
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.