The Fixed Point interview question is a search problem involving a sorted array of unique integers. A "fixed point" is an index such that . You are asked to find the smallest index that is a fixed point. If no such index exists, you should return -1.
Uber and other companies use the Fixed Point coding problem to see if a candidate can recognize an opportunity for Binary Search in a non-obvious context. Since the array is sorted and elements are unique, the difference is a monotonically non-decreasing function. This property allows you to discard half of the search space in each step, reaching a logarithmic time complexity ().
This problem is a direct application of the Binary Search interview pattern.
left = 0, right = n - 1.arr[mid] == mid, this is a potential answer. However, we need the smallest index, so we record this index and continue searching the left half: ans = mid, right = mid - 1.arr[mid] < mid, then for all , arr[j] must also be less than (because the values can't "catch up" to the indices without a larger gap). Move left = mid + 1.arr[mid] > mid, then for all , arr[j] will always be greater than . Move right = mid - 1.Array: [-10, -5, 2, 3, 7]
left=0, right=4, mid=2. arr[2] = 2. Smallest found so far: 2. Search left (right = 1).left=0, right=1, mid=0. arr[0] = -10 < 0. Search right (left = 1).left=1, right=1, mid=1. arr[1] = -5 < 1. Search right (left = 2).arr[mid] with a target value instead of its own index mid.Binary Search is useful whenever the "error" or "difference" between your current value and a target is monotonic. In this Array interview pattern, the monotonic value is arr[i] - i.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Kth Missing Positive Number | Easy | Solve | |
| Search Insert Position | Easy | Solve | |
| Binary Search | Easy | Solve | |
| Check If a Number Is Majority Element in a Sorted Array | Easy | Solve | |
| Find Smallest Letter Greater Than Target | Easy | Solve |