Find missing value in sorted consecutive array
AristaSoftware Engineer · Technical ScreenLast updated
This question evaluates understanding of array properties and algorithmic complexity, specifically detecting a single missing element in a sorted consecutive sequence within the Coding & Algorithms category.
Given a sorted array nums of distinct integers. The numbers are supposed to form a consecutive sequence starting from some unknown integer start:
- If nothing is missing, the sequence would be:
start, start+1, start+2, ..., start+n-1wheren = len(nums). - But at most one number from this sequence may be missing in
nums.
Return the missing number if one exists; otherwise return null (or -1, but be consistent).
Requirements
- Time complexity must be O(log n).
- Space complexity must be O(1).
Examples
nums = [4,5,6,8,9]→ missing is7nums = [-3,-2,-1,0,1]→ no missing → returnnullnums = [10,11,12,13,15]→ missing is14
Notes
startcan be any integer (including negative).numsis already sorted ascending and contains no duplicates.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.