🏷️ 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

ConceptWhy It Matters
Divide and conquerCore algorithm pattern
Off-by-one errorsCareful pointer handling
O(log n) awarenessEfficiency 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:

Steplowhighmidnums[mid]Action
105255 < 7 β†’ low = 3
235499 > 7 β†’ high = 3
33337βœ… 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

Scroll to Top