Missing Ranges

MediumArrays & Hashingsimulation

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given an inclusive range [lower, upper] and a sorted array nums of **distinct** integers that all lie inside that range. Some integers of the range are missing from nums. Return the shortest list of ranges that covers every missing integer exactly once. Each range is a pair [a, b] with a <= b meaning all integers from a to b inclusive are missing (a single missing number x is [x, x]). Ranges must be listed in ascending order and must not overlap or touch. If nothing is missing, return an empty list.

Examples

Input: nums = [0,1,3,50,75], lower = 0, upper = 99
Output: [[2,2],[4,49],[51,74],[76,99]]
The gaps are 2, 4 through 49, 51 through 74, and 76 through 99.
Input: nums = [-1], lower = -1, upper = -1
Output: []
The only number in the range is present.

Constraints

  • -10^9 <= lower <= upper <= 10^9
  • 0 <= nums.length <= 100
  • lower <= nums[i] <= upper
  • All values of `nums` are distinct and sorted in ascending order

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More simulation problems