"""Interpolation search.

Idea
----
Binary search always probes the middle. If the sorted values are spread
roughly evenly (like page numbers or uniformly random IDs), you can guess
much better: estimate where the target "should" be from its value, the way
you open a dictionary near the back for a word starting with "w".

    pos = lo + (target - a[lo]) * (hi - lo) // (a[hi] - a[lo])

Complexity
----------
Time:  O(log log n) expected on uniformly distributed keys, O(n) worst case
       (for example exponentially growing values)
Space: O(1)

When to use
-----------
Large, sorted, numeric, evenly spread keys. Otherwise binary search is safer.
"""

from collections.abc import Sequence


def interpolation_search(items: Sequence[int], target: int) -> int:
    """Return an index of ``target`` in sorted numeric ``items`` or -1."""
    lo, hi = 0, len(items) - 1
    while lo <= hi and items[lo] <= target <= items[hi]:
        if items[hi] == items[lo]:
            return lo if items[lo] == target else -1
        pos = lo + (target - items[lo]) * (hi - lo) // (items[hi] - items[lo])
        if items[pos] == target:
            return pos
        if items[pos] < target:
            lo = pos + 1
        else:
            hi = pos - 1
    return -1
