KatInterview

Uncovered Integer Gaps Within a Closed Window

Arrays & HashingAuto-graded · 26 test cases
EasyAuto-gradedArrays & Hashingarraysintervalstwo-pointers

Last updated

Scan a sorted list of distinct integers once to report every maximal run of integers in a closed window that the list skips, formatting single gaps as one-element lists.

You are given a sorted list values of distinct integers and two integers low and high. Together, low and high define the closed window of integers from low to high, including both endpoints.

Find every maximal run of consecutive integers inside the window that is not present in values. A run is maximal if it cannot be extended by another missing integer on either side while staying inside the window.

Entries of values that lie outside the window play no role and must be ignored.

Output format

Return a list of runs in ascending order. Write each run as:

  • [x] when the run consists of exactly one integer x
  • [start, end] when the run contains two or more integers, where start < end

If nothing in the window is missing, return an empty list. If low > high, the window is empty and the result is an empty list.

Constraints

  • values is sorted in ascending order and contains no duplicates.
  • values may be empty, and may contain negative numbers.
  • Entries of values may be smaller than low or larger than high.
  • The bounds low and high may be very far apart, so the solution should not iterate over every integer in the window.
  • A single-integer run must be written as [x], never [x, x].

Examples

Example 1

Input: find_uncovered_gaps([3, 5, 10], 0, 9)

Output: [[0, 2], [4], [6, 9]]

Hints

Hint 1

Keep one cursor holding the smallest integer in the window that has not yet been accounted for; start it at `low`.

Hint 2

For each in-window value `x`, if `x` is larger than the cursor, everything from the cursor up to `x - 1` is missing. Then move the cursor to `x + 1`.

Hint 3

Skip values below `low`, stop at the first value above `high`, and after the loop report whatever remains from the cursor to `high`. Remember to use the one-element format when a gap has length one.