My first instinct was to just compute the expected sum and subtract, which is O(n) and they shut that down immediately.
Use binary search to find the missing number by comparing the expected value at each index with the actual value. The key insight is that in a consecutive sequence starting at arr[0], the value at index i should be arr[0] + i. If the array is missing one number, all elements before the missing number satisfy this, and all after do not. Binary search for the first index where arr[i] != arr[0] + i; if found, the missing number is arr[0] + i, otherwise return null.
Pro tip: Explicitly state the invariant: 'For all indices less than the missing index, arr[i] == arr[0] + i; for all indices greater, arr[i] > arr[0] + i.' This shows you understand why binary search works and helps avoid off-by-one errors.
Confirm that the array is sorted, contains distinct integers, and that exactly one number may be missing (or none). Discuss edge cases: empty array, single element, missing number at the beginning or end.
For a consecutive sequence starting at arr[0], the expected value at index i is arr[0] + i. The invariant: if a missing number exists, there is an index m such that for all i < m, arr[i] == arr[0] + i, and for all i >= m, arr[i] > arr[0] + i.
Initialize low = 0, high = n-1. While low <= high, compute mid. If arr[mid] == arr[0] + mid, the missing number is to the right, so set low = mid + 1. Else, the missing number is at or to the left, so set high = mid - 1. After the loop, low is the first index where the condition fails.
If low < n, then the missing number is arr[0] + low. Otherwise, no missing number exists, so return null.
Confirm O(log n) time and O(1) space. Walk through examples: [1,2,4,5] -> 3; [1,2,3,4] -> null; [2,3,4,5] -> null (but note: if missing at end, e.g., [1,2,3,5] -> 4).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.