Find Target Index in Sorted Array
Heaps, Sorting & SearchAuto-graded · 24 test casesLast updated
Tests ability to implement iterative binary search with correct loop invariants, boundary handling, and termination conditions.
Write a function that searches for a target value in a sorted array of integers and returns its index. If the target appears multiple times, return any valid index. If the target is not present, return -1.
The array is sorted in ascending order and may contain duplicates.
Your implementation must use an iterative approach with logarithmic time complexity.
Constraints
0 ≤ len(nums) ≤ 10⁵-10⁹ ≤ nums[i] ≤ 10⁹-10⁹ ≤ target ≤ 10⁹numsis sorted in non-decreasing order
Examples
Example 1
Input: find_target_index([1, 3, 5, 7, 9], 5)
Output: 2
Example 2
Input: find_target_index([1, 3, 5, 7, 9], 4)
Output: -1
Example 3
Input: find_target_index([], 10)
Output: -1
Example 4
Input: find_target_index([2, 2, 2, 2], 2)
Output: 1
Hints
Hint 1
Maintain a search window that shrinks by half each iteration by comparing the middle element to the target.
Hint 2
Use an inclusive range [left, right] with the loop condition left <= right, and update boundaries with mid ± 1 to avoid infinite loops.
Hint 3
Calculate mid as left + (right - left) // 2 to prevent potential overflow issues in other languages.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Binary search exploits the sorted nature of the input to eliminate half of the remaining candidates with each comparison. We maintain a search window [left, right] that is guaranteed to contain the target if it exists anywhere in the array.
At each step, we examine the middle element of the current window. If it matches the target, we return immediately. If the middle element is smaller than the target, the target must lie in the right half, so we move left to mid + 1. If the middle element is larger, the target must lie in the left half, so we move right to mid - 1.
The loop continues while left <= right. When the window becomes empty (left > right), we know the target is not present and return -1.
Correctness
The algorithm maintains the invariant that if the target exists in the array, its index lies within [left, right].
Initially, [0, len(nums) - 1] covers the entire array, so the invariant holds. Each iteration either finds the target or excludes mid and everything on one side of it—preserving the invariant because we only exclude elements we've proven cannot be the target. Since we increment or decrement by at least one each time (mid ± 1), the window strictly shrinks and the loop terminates.
Using the inclusive window with left <= right ensures single-element windows are checked. The mid ± 1 updates prevent infinite loops that would occur with left = mid or right = mid in an inclusive range.
Complexity
Time: O(log n). The search space halves with each iteration, yielding at most ⌊log₂ n⌋ + 1 comparisons.
Space: O(1). Only a constant number of index variables are maintained.
Solution
from typing import List
def find_target_index(nums: List[int], target: int) -> int:
left, right = 0, len(nums) - 1
while left <= right:
mid = left + (right - left) // 2
if nums[mid] == target:
return mid
if nums[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1