KatInterview

Find Gaps in Traffic Segment Assignments

Arrays & HashingAuto-graded · 18 test cases
MediumAuto-gradedArrays & Hashingsetsfilteringranges

Last updated

Tests set-based membership filtering over a bounded integer range, with duplicate and out-of-bounds input handling.

Problem

A traffic routing system divides incoming requests into num_slots segments, numbered 0 through num_slots - 1. Each routing rule claims some of those segments: one rule acts as a baseline and the rest are variants, each holding a list of segment numbers.

Your job is to find every segment that no rule has claimed.

Function Signature

def find_unclaimed_slots(
    num_slots: int,
    baseline: list[int],
    variants: list[list[int]]
) -> list[int]:

Input

  • num_slots — non-negative integer; defines the valid segment range [0, num_slots - 1].
  • baseline — list of integer segment IDs claimed by the baseline rule.
  • variants — list of lists; each inner list contains integer segment IDs claimed by one variant rule.

Output

A list of integers — the segment IDs in [0, num_slots - 1] that appear in neither baseline nor any variant — sorted in ascending order, with no duplicates.

Rules

  • A segment ID is valid only if it falls within [0, num_slots - 1]. IDs outside this range (negative values or values >= num_slots) must be silently ignored.
  • The same ID may appear more than once within a single rule's list, or across multiple rules. Treat repeated claims of the same segment as a single claim — they do not cause errors.
  • An inner list inside variants may be empty; that is not an error.

Constraints

  • 1 <= num_slots <= 100,000
  • 0 <= len(baseline) <= 100,000
  • 0 <= len(variants) <= 1,000
  • 0 <= sum(len(v) for v in variants) <= 100,000
  • Individual segment IDs in the input can be any 32-bit signed integer.

Examples

Example 1

Input: find_unclaimed_slots(10, [0, 2, 2, 15], [[3, 4], [4, 7]])

Output: [1, 5, 6, 8, 9]

Example 2

Input: find_unclaimed_slots(4, [0, 1], [[2, 3]])

Output: []

Hints

Hint 1

Start by collecting every valid, in-range segment ID from all rules into a single structure that makes membership checks cheap — then decide what is missing from the full range.

Hint 2

You do not need to sort the inputs. If you scan the full range 0..num_slots-1 in order and skip anything already claimed, the output comes out sorted for free.

Hint 3

Watch out for negative IDs: if you use an array indexed by segment ID, a negative value will silently wrap around in Python and corrupt your results. A bounds check before any indexing or insertion prevents this.