Missing Ranges
Problem
You are given a sorted array of distinct integers nums and two integers lower and upper with lower <= upper. Every element of nums lies within [lower, upper]. A number in [lower, upper] is missing if it does not appear in nums. Return the missing numbers grouped into maximal contiguous ranges, as a list of [start, end] pairs in increasing order. Each pair covers the inclusive range of missing values from start to end.
- The array may be empty and has at most a few hundred elements.
loweranduppercan be anywhere in the signed 32-bit range, so the interval may span billions of values.- Elements are strictly increasing and all lie inside
[lower, upper].
Examples
nums = [12, 14, 15, 30], lower = 10, upper = 40→[[10, 11], [13, 13], [16, 29], [31, 40]]. A head gap before 12, gaps between present values, and a tail gap up to 40.nums = [], lower = 7, upper = 7→[[7, 7]]. Nothing is present, so the single value 7 is missing.nums = [5], lower = 5, upper = 5→[]. The only value in range is present.
Hints
- A gap exists between two consecutive present values
aandbexactly whenb - a > 1; the gap is[a + 1, b - 1]. - The first gap may start at
lowerand the last may end atupper. Can you make those look like ordinary gaps? - Treat
lower - 1andupper + 1as virtual sentinels on each side of the array, then apply the gap rule uniformly to every adjacent pair.
Last reviewed 8 September 2026
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.