π·οΈ Category: Algorithms / Search | π― Difficulty: Easy | π₯ Frequency: β
β
β
β
β
π Problem Statement
Given a sorted list and a target, return the index of the target using Binary Search. Return -1 if not found.
π₯ Input β nums = [1, 3, 5, 7, 9, 11], target = 7
π€ Output β 3
π₯ Input β nums = [1, 3, 5, 7, 9, 11], target = 6
π€ Output β -1
π§ What the Interviewer is Testing
| Concept | Why It Matters |
|---|---|
| Divide and conquer | Core algorithm pattern |
| Off-by-one errors | Careful pointer handling |
| O(log n) awareness | Efficiency over linear search |
β οΈ Key requirement: Array must be sorted. Always mention this constraint.
π‘ Approach
Keep two pointers: low and high
While low <= high:
Find mid = (low + high) // 2
If nums[mid] == target β return mid
If nums[mid] < target β search right half (low = mid + 1)
If nums[mid] > target β search left half (high = mid - 1)
Return -1 if not found
β Optimal Solution
def binary_search(nums: list, target: int) -> int:
low, high = 0, len(nums) - 1
while low <= high:
mid = (low + high) // 2
if nums[mid] == target:
return mid
elif nums[mid] < target:
low = mid + 1 # target in right half
else:
high = mid - 1 # target in left half
return -1 # not found
# ββ Test Cases βββββββββββββββββββββββββββββββββ
print(binary_search([1, 3, 5, 7, 9, 11], 7)) # β
3
print(binary_search([1, 3, 5, 7, 9, 11], 6)) # β
-1
print(binary_search([1], 1)) # β
0
π Step-by-Step Walkthrough
nums = [1, 3, 5, 7, 9, 11], target = 7:
| Step | low | high | mid | nums[mid] | Action |
|---|---|---|---|---|---|
| 1 | 0 | 5 | 2 | 5 | 5 < 7 β low = 3 |
| 2 | 3 | 5 | 4 | 9 | 9 > 7 β high = 3 |
| 3 | 3 | 3 | 3 | 7 | β Found! Return 3 |
π Complexity Analysis
ββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββ
β Time O(logn) β Search space halves each iteration β
β Space O(1) β Only two pointers used, no extra memory β
ββββββββββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββ
π€ How to Explain in the Interview
βBinary search works on sorted arrays by repeatedly halving the search space. I maintain low and high pointers and check the middle element. If it matches I return the index. If the target is larger I move low up, if smaller I move high down. This gives O(log n) compared to linear searchβs O(n).β
βBinary search works on sorted arrays by repeatedly halving the search space. I maintain low and high pointers and check the middle element. If it matches I return the index. If the target is larger I move low up, if smaller I move high down. This gives O(log n) compared to linear searchβs O(n).β
π Common Follow-up Questions
Q1 β Recursive version?
def binary_search_recursive(nums, target, low=0, high=None):
if high is None:
high = len(nums) - 1
if low > high:
return -1
mid = (low + high) // 2
if nums[mid] == target:
return mid
elif nums[mid] < target:
return binary_search_recursive(nums, target, mid + 1, high)
else:
return binary_search_recursive(nums, target, low, mid - 1)
Q2 β Find first/last occurrence of duplicate?
# For first occurrence, keep searching left even after finding target
# For last occurrence, keep searching right
π Set 1 of 4 β Python Interview Prep Series β devprepguide.com
