KatInterview

Track Covered Spans Under Add, Erase and Check Commands

Data Structure DesignAuto-graded · 18 test cases
HardAuto-gradedData Structure Designintervalsbinary-searchsimulation

Last updated

Maintain a set of half-open real intervals under add, remove and fully-covered queries, merging touching spans and splitting spans on removal.

You are given a sequence of commands to run, in order, against an initially empty collection of covered stretches of the number line. Every stretch is a half-open interval [left, right): it includes left and excludes right, and it contains every real number in between, not only integers.

Each command is one of three kinds:

  • "addRange" with arguments [left, right]: mark every real number in [left, right) as covered. Covered stretches that overlap or touch the new one become one continuous stretch. For example, covering [1, 5) and then [5, 10) leaves [1, 10) covered.
  • "removeRange" with arguments [left, right]: mark every real number in [left, right) as no longer covered. This can split a stretch in two. Removing [5, 8) from [1, 10) leaves [1, 5) and [8, 10). Removing from an area that is not covered changes nothing.
  • "queryRange" with arguments [left, right]: report whether every real number in [left, right) is currently covered. This command does not change the collection.

Implement a function that takes the list of command names and the parallel list of argument pairs, runs the commands in order, and returns a list of results with one entry per command:

  • for "addRange" and "removeRange", the entry is None;
  • for "queryRange", the entry is True or False.

A query on an empty collection returns False.

Constraints

  • operations and arguments have the same length, between 1 and 20,000.
  • Each operation name is exactly "addRange", "removeRange" or "queryRange".
  • Each element of arguments is a pair [left, right] with 0 <= left < right <= 10^9.
  • The collection starts empty.

Examples

Example 1

Input: track_covered_spans(['addRange', 'removeRange', 'queryRange', 'queryRange', 'queryRange'], [[10, 20], [14, 16], [10, 14], [13, 15], [16, 17]])

Output: [None, None, True, False, True]

Example 2

Input: track_covered_spans(['addRange', 'addRange', 'queryRange', 'removeRange', 'queryRange', 'queryRange'], [[1, 5], [5, 10], [1, 10], [3, 7], [1, 3], [7, 10]])

Output: [None, None, True, None, True, True]

Example 3

Input: track_covered_spans(['queryRange', 'addRange', 'queryRange', 'removeRange', 'queryRange'], [[0, 100], [0, 100], [0, 100], [20, 80], [10, 20]])

Output: [False, None, True, None, True]

Hints

Hint 1

Keep the covered area as a sorted list of disjoint stretches, so each command only needs to look at the stretches near its endpoints. Keep a parallel list of start values so you can binary search.

Hint 2

Because touching stretches must merge, a stretch ending exactly at the new left end, or starting exactly at the new right end, must be absorbed when adding. Scan forward from the first stretch that could touch the new one while its start is at most the new right end, and widen the new stretch as you go.

Hint 3

For removal, find the first stretch that might reach past `left`, then walk over every stretch starting before `right`. Each of those leaves at most a left remainder `[a, left)` and a right remainder `[right, b)`. For a query, only the single stretch whose start is the largest one not exceeding `left` can possibly cover the range.