The Kth Missing Positive Number interview question asks you to find the positive integer that is not present in a given sorted array. For example, in [2, 3, 4, 7, 11] with , the missing numbers are 1, 5, 6, 8, 9, 10... the one is 9.
Companies like Uber and Meta ask this to see if you can optimize a linear search () into a logarithmic search (). Because the array is sorted, you can calculate how many numbers are missing before any given index. This is a classic Binary Search interview pattern.
This problem is best solved using Binary Search on Indices.
i, the value should be i + 1 if no numbers were missing.arr[i] is arr[i] - (i + 1).left such that arr[left] - (left + 1) >= k.left + k.arr = [2, 3, 4, 7, 11], k = 5
left + k = 4 + 5 = 9.Whenever you have a sorted array and a question about "gaps" or "missing items," try to define a property f(i) that counts gaps up to index i. If f(i) is monotonic, you can always use Binary Search. This is a powerful Array interview pattern.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Search Insert Position | Easy | Solve | |
| Find Smallest Letter Greater Than Target | Easy | Solve | |
| Binary Search | Easy | Solve | |
| Fixed Point | Easy | Solve | |
| Check If a Number Is Majority Element in a Sorted Array | Easy | Solve |