Back to Practice
#0260
Find First and Last Position
MediumDSA28 min20 XP
Problem
Given sorted nums and target, return [first_index, last_index]. If target is missing, return [-1, -1].
Why This Matters
Boundary binary search is the difference between knowing binary search roughly and using it safely in production code.
Function Signature
def search_range(nums, target):
Examples
Example 1
Inputnums = [5,7,7,8,8,10], target = 8
Output[3, 4]
8 appears from index 3 through index 4.
Constraints
- Return the exact requested value.
- Handle edge cases cleanly.
- Use the intended DSA pattern when brute force would scale poorly.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Repeated target
Input[5,7,7,8,8,10], target = 8
Expected[3, 4]
The lower bound of 8 is 3; the lower bound of 9 is 5.
Missing target
Input[5,7,7,8,8,10], target = 6
Expected[-1, -1]
6 is not found.
Hidden Test Categories
all values equal targettarget at beginningempty array