Find the Slot for a Value in a Sorted List of Distinct Integers
Heaps, Sorting & SearchAuto-graded · 26 test casesLast updated
Tests explicit binary search on a sorted array: find a value's index or the position where it would be inserted, in logarithmic time.
You are given a list of integers values sorted in strictly ascending order (all elements are distinct) and a single integer query.
Return the index at which query sits in the list. If query is not in the list, return the index at which it would have to be inserted so that the list stays in ascending order.
Implement the search yourself with an explicit binary search. Do not use bisect or any similar library helper. Your solution should run in O(log n) time.
Output rules
- If
queryis present, return its index. - If
queryis absent, return the number of elements that are strictly smaller thanquery. This is the index where it would be inserted. - If
queryis larger than every element, returnlen(values). - If
queryis smaller than every element, return0. - If
valuesis empty, return0.
Constraints
0 <= len(values) <= 10^4-10^4 <= values[i], query <= 10^4valuesis sorted in strictly ascending order (no duplicates).- The returned value is an integer in the range
[0, len(values)].
Examples
Example 1
Input: find_insertion_index([1, 3, 5, 6], 5)
Output: 2
Example 2
Input: find_insertion_index([1, 3, 5, 6], 2)
Output: 1
Example 3
Input: find_insertion_index([1, 3, 5, 6], 7)
Output: 4
Example 4
Input: find_insertion_index([1, 3, 5, 6], 0)
Output: 0
Hints
Hint 1
The answer is the first index whose element is greater than or equal to the query, or len(values) if no such element exists.
Hint 2
Keep two pointers, low and high, around the part of the list not yet ruled out. Compare the middle element to the query and discard the half that cannot contain the answer.
Hint 3
When the loop ends without finding the query, every index left of low holds a smaller value and every index from low onward holds a larger value, so low is the insertion index. Do not return high or mid.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Because the list is sorted, the answer is the first index i where values[i] >= query. If the query is present, that is its own position. If it is absent, that is where it belongs. If every element is smaller, the answer is len(values).
Use a binary search over the closed range [low, high], starting with low = 0 and high = len(values) - 1:
- Compute
mid. - If
values[mid] == query, returnmid. - If
values[mid] < query, the answer lies to the right, so setlow = mid + 1. - Otherwise the answer is at
midor to its left, so sethigh = mid - 1. - When
low > high, returnlow.
Why it is correct
The loop maintains this invariant: every index before low holds a value smaller than query, and every index after high holds a value larger than query. It holds at the start because both of those regions are empty. Each update extends one of the regions correctly, using the sorted order. Equality returns immediately, which is safe because values are distinct. The interval shrinks every iteration, so the loop terminates with low == high + 1. At that point everything before low is smaller and everything from low onward is larger, so low is exactly the insertion index. This also covers the empty list (loop never runs, returns 0), a query below everything (returns 0), and a query above everything (returns len(values)).
Complexity
- Time: O(log n), since the search range halves each iteration.
- Space: O(1).
Reference solution
from typing import List
def find_insertion_index(values: List[int], query: int) -> int:
low, high = 0, len(values) - 1
while low <= high:
mid = (low + high) // 2
if values[mid] == query:
return mid
if values[mid] < query:
low = mid + 1
else:
high = mid - 1
return low