Tasks /
Algorithms /
Binary search
Statement
medium
Binary search
Implement `binary_search(items, target)` for a sorted list.
Return the index of the **first** occurrence, or `-1` when the target is absent.
Example: `binary_search([1, 2, 2, 2, 3], 2)` → `1`.
A plain binary search may return any of the occurrences — here it must be the
first one. Cover an empty list, a missing element and duplicates.
AI mentor
The AI mentor is available after you log in: it sees your attempt history and does not spend resources on anonymous requests.
Log in and ask
Hints
1 of 3 unlocked · attempts: 0
Hint 1
How does "first occurrence" differ from "any occurrence"? What should happen when
`items[middle] == target`?
Hint 2 — unlocks after 1 attempt
Hint 3 — unlocks after 3 attempts