Binary Search Recursive Approach in DSA Go - Time & Space Complexity
We want to understand how fast the recursive binary search finds a number in a sorted list.
How does the time it takes change as the list gets bigger?
Analyze the time complexity of the following code snippet.
func binarySearch(arr []int, target, low, high int) int {
if low > high {
return -1
}
mid := low + (high - low) / 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)
}
}
This code searches for a target number in a sorted list by repeatedly dividing the search range in half.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive call that halves the search range each time.
- How many times: The search range is halved each call, repeating until the range is empty or target found.
Each step cuts the list size roughly in half, so the number of steps grows slowly as the list grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 4 steps |
| 100 | About 7 steps |
| 1000 | About 10 steps |
Pattern observation: Doubling the list size adds only one extra step.
Time Complexity: O(log n)
This means the search time grows slowly, adding just a few more steps even if the list gets much bigger.
[X] Wrong: "Binary search checks every item one by one, so it takes as long as the list size."
[OK] Correct: Binary search skips half the list each time, so it does not check every item, making it much faster.
Understanding binary search time helps you explain efficient searching clearly and confidently in interviews.
"What if the list was not sorted? How would the time complexity of searching change?"
Practice
binary search recursive approach on a sorted array?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 BQuick Check:
Binary search = divide and conquer [OK]
- Trying to search linearly instead of dividing
- Not using recursion properly
- Ignoring that array must be sorted
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 CQuick Check:
Parameters include low and high indices [OK]
- Omitting low and high indices in parameters
- Adding unnecessary parameters like mid
- Using wrong return types
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)
}
}
func main() {
arr := []int{2, 4, 6, 8, 10}
result := binarySearch(arr, 6, 0, len(arr)-1)
fmt.Println(result)
}Solution
Step 1: Trace the recursive calls for target 6
Initial call: low=0, high=4, mid=2, arr[2]=6 equals target, so return 2 immediately.Step 2: Confirm output
Since target found at index 2, output is 2.Final Answer:
2 -> Option DQuick Check:
Target 6 found at index 2 [OK]
- Confusing index with value
- Not updating low and high correctly
- Returning wrong index or -1 incorrectly
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)
}
}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 AQuick Check:
Base case must allow low == high search [OK]
- Stopping recursion too early with wrong base case
- Incorrect mid calculation causing infinite recursion
- Returning wrong default value when not found
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 AQuick Check:
Check left side for first occurrence [OK]
- Returning first found index without checking duplicates
- Searching right half instead of left for first occurrence
- Using linear search unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
