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 > end), meaning the target is not found, or when the middle element equals the target, meaning the search is successful.
Click to reveal answer
beginner
Why must the input list be sorted for binary search to work?
Because binary search decides which half to search based on comparisons, the list 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 recursion help in implementing binary search?
Recursion simplifies the code by letting the function call itself with a smaller search range until it finds the target or the range is empty.
Click to reveal answer
What should happen if the middle element is equal to the target in recursive binary search?
AReturn -1
BSearch the left half
CReturn the middle index
DSearch the right half
✗ Incorrect
If the middle element matches the target, the search is successful and the middle index is returned.
What condition ends the recursive binary search when the target is not found?
Amiddle < target
Bstart == end
Cmiddle == target
Dstart > end
✗ Incorrect
When the start index becomes greater than the end index, it means the search range is empty and the target is not in the list.
Which of these is required for binary search to work correctly?
AThe list must be sorted
BThe list must be unsorted
CThe list must contain duplicates
DThe list must be empty
✗ Incorrect
Binary search requires a sorted list to decide which half to search next.
What is the time complexity of binary search?
AO(n)
BO(log n)
CO(n^2)
DO(1)
✗ Incorrect
Binary search cuts the search space in half each step, leading to O(log n) time complexity.
In recursive binary search, what parameters typically change with each recursive call?
AThe search range (start and end indices)
BThe target value
CThe entire list
DThe middle element
✗ Incorrect
Each recursive call narrows the search range by updating the start or end index.
Explain how recursive binary search works step-by-step on a sorted list.
Think about how the list is split and how the function calls itself.
You got /4 concepts.
Describe why the list must be sorted for binary search and what happens if it is not.
Consider how binary search decides which half to search next.
You got /3 concepts.
Practice
(1/5)
1. What is the main idea behind the binary search recursive approach on a sorted array?
easy
A. Sort the array first and then search linearly
B. Divide the array into halves and search recursively in the half where the target may exist
C. Search each element one by one from start to end
D. Use a queue to store elements and search iteratively
Solution
Step 1: Understand binary search logic
Binary search works by dividing the sorted array into two halves and deciding which half to search next based on comparison with the middle element.
Step 2: Apply recursion in binary search
The recursive approach 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 half where the target may exist -> Option B
2. Which of the following is the correct function signature for a recursive binary search in Go?
easy
A. func binarySearch(arr []int, target int, mid int) int
B. func binarySearch(arr []int, target int) int
C. func binarySearch(arr []int, target int, low int, high int) int
D. func binarySearch(arr []int) int
Solution
Step 1: Identify parameters needed for recursion
Recursive binary search needs the array, target, and the current low and high indices to know the search range.
Step 2: Check function signatures
func binarySearch(arr []int, target int, low int, high int) int includes low and high indices, which are essential for recursion. Others miss these or have incorrect parameters.
Final Answer:
func binarySearch(arr []int, target int, low int, high int) int -> Option C
Quick Check:
Parameters include low and high indices [OK]
Hint: Recursive binary search needs low and high index parameters [OK]
Common Mistakes:
Omitting low and high indices in parameters
Adding unnecessary parameters like mid
Using wrong return types
3. What will be the output of the following Go code snippet?
Hint: Check mid index value first to find target quickly [OK]
Common Mistakes:
Confusing index with value
Not updating low and high correctly
Returning wrong index or -1 incorrectly
4. Identify the error in the following recursive binary search function and select the fix:
func binarySearch(arr []int, target, low, high int) int {
if low >= high {
return -1
}
mid := (low + high) / 2
if arr[mid] == target {
return mid
} else if arr[mid] > target {
return binarySearch(arr, target, low, mid-1)
} else {
return binarySearch(arr, target, mid+1, high)
}
}
medium
A. Change condition to if low > high instead of low >= high
B. Change mid calculation to mid := (low + high + 1) / 2
C. Return 0 instead of -1 when not found
D. Remove else if and use only if statements
Solution
Step 1: Analyze base case condition
The base case should stop recursion when low is greater than high, meaning no elements left to search. Using low >= high skips searching when low equals high, missing that element.
Step 2: Correct the condition
Changing to low > high allows searching the last element when low equals high.
Final Answer:
Change condition to if low > high instead of low >= high -> Option A
Quick Check:
Base case must allow low == high search [OK]
Hint: Base case should be low > high, not low >= high [OK]
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 found, check if previous element is same; if yes, search left half recursively
B. Return the first found index immediately without further checks
C. Search only the right half recursively after finding target
D. Use linear search after binary search to find first occurrence
Solution
Step 1: Understand duplicates in binary search
Standard binary search returns any occurrence of target. To find first occurrence, we must check if earlier elements are also equal.
Step 2: Modify recursion to check left side
When target found at mid, check if mid is first index or previous element is different. If previous element is same, recursively search left half to find earlier occurrence.
Final Answer:
When target found, check if previous element is same; if yes, search left half recursively -> Option A
Quick Check:
Check left side for first occurrence [OK]
Hint: On finding target, move left if previous equals target [OK]
Common Mistakes:
Returning first found index without checking duplicates
Searching right half instead of left for first occurrence
Using linear search unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.