Find missing ranges in interval
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates a candidate's proficiency in array traversal, interval reasoning, handling edge cases including empty inputs and 32-bit integer boundary conditions, and enforcing time and space complexity constraints.
Constraints
- -2^31 <= lower <= upper <= 2^31 - 1
- 0 <= nums.length <= 100
- lower <= nums[i] <= upper for every i
- nums is sorted in strictly increasing order (all elements unique)
Examples
Input: ([0, 1, 3, 50, 75], 0, 99)
Expected Output: ['2', '4->49', '51->74', '76->99']
Explanation: Within [0,99]: 2 is missing (single), 4..49 missing, 51..74 missing, and 76..99 missing up to the upper bound.
Input: ([], 1, 1)
Expected Output: ['1']
Explanation: nums is empty and the interval [1,1] is a single integer, so the whole interval is one missing single-number range.
Hints
- Walk through the gaps between consecutive 'covered' points. Seed a `prev` value at `lower - 1` so the first real gap is measured from the left boundary, and treat `upper + 1` as a virtual element after the last real one so the final gap up to `upper` is captured.
- A missing range exists between two adjacent covered values `prev` and `cur` exactly when there is at least one integer strictly between them — i.e. when `cur - prev >= 2`. The missing range is then `[prev + 1, cur - 1]`.
- To avoid 32-bit overflow at INT_MIN / INT_MAX, compare the gap with subtraction (`cur - prev >= 2`) rather than computing `prev + 1` or `cur + 1` before you know a gap exists, and only form the boundary values once you've confirmed the gap.