Ctrl K

Find in a Sorted List

Easy
An archive keeps a list of record numbers ids, sorted in ascending order with no repeats. You need to find where the number wanted is stored. Return the index of wanted in ids (0-based), or -1 if it is not there. Scanning the whole list is too slow: your solution must run in O(log n).
Example 1
ids=[-7, 2, 15, 40, 41, 300], wanted=40 → 3

Number 40 sits at index 3.

Example 2
ids=[-7, 2, 15, 40, 41, 300], wanted=16 → -1

There is no 16 in the list, so the answer is -1.

Example 3
ids=[5], wanted=5 → 0
Constraints
  • 1 ≤ len(ids) ≤ 10⁴
  • -10⁴ ≤ ids[i], wanted ≤ 10⁴
  • All ids[i] are distinct and strictly increasing
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSlot on a Sorted Shelf EasyThe Lost Ticket EasyValues in Both Lists EasyShared Values With Repeats EasyNext Letter, Wrapping Around Easy
loading Python…
Input
ids=[-7, 2, 15, 40, 41, 300], wanted=40
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 111 tests on the server, hidden ones included.