Find Gaps in Traffic Segment Assignments
Arrays & HashingAuto-graded · 18 test casesLast 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
variantsmay be empty; that is not an error.
Constraints
1 <= num_slots <= 100,0000 <= len(baseline) <= 100,0000 <= len(variants) <= 1,0000 <= 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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is a straightforward membership problem over a small, fully known universe.
Plan
- Allocate an empty set
claimed. - Walk every ID in
baselineand in every inner list ofvariants. For each ID, check whether it falls in[0, num_slots - 1]. If it does, add it toclaimed; otherwise skip it. Duplicates are harmless — adding the same value to a set twice has no effect. - Iterate
range(num_slots)in order and emit every index that is not inclaimed.
Why it is correct
- Soundness: A segment appears in the output only if it is a valid index that was never added to
claimed. It can only be added when some input ID equals it and passes the bounds check, so every returned value is genuinely unclaimed. - Completeness: Every index in
[0, num_slots - 1]is visited exactly once in the final scan, so no unclaimed segment is ever missed. - Ordering and uniqueness: The scan visits indices in strictly increasing order and each index is visited once, so the output is already sorted and duplicate-free — no extra
sorted()call needed. - Out-of-bounds safety: Any ID that is negative or
>= num_slotsfails the bounds check before it touchesclaimed. This is especially important for negative IDs: a Python list indexed by a negative integer wraps around to the end, which would silently corrupt an array-based solution. The explicit guard prevents that.
Complexity
Let B = num_slots and N = len(baseline) + sum(len(v) for v in variants).
- Time: O(N + B). Each input ID costs O(1) to check and insert. The final scan costs O(B).
- Space: O(min(N, B)) for
claimed, plus O(output size) for the returned list.
Edge cases
- All segments claimed → returns
[]. - Nothing claimed (
baseline = [],variants = []) → returns[0, 1, ..., num_slots - 1]. - Every ID out of range → behaves identically to nothing claimed.
- Empty inner lists inside
variants→ the inner loop does nothing; no error. num_slots = 0→range(0)is empty, returns[]naturally.- Heavy duplication (same ID repeated 100,000 times) → still linear time and constant extra memory.
Reference solution
from typing import List
def find_unclaimed_slots(num_slots: int, baseline: List[int], variants: List[List[int]]) -> List[int]:
# Collect every valid claimed segment ID into a set.
# Using a set handles duplicates automatically and gives O(1) lookups.
claimed = set()
for seg in baseline:
if 0 <= seg < num_slots:
claimed.add(seg)
for variant in variants:
for seg in variant:
if 0 <= seg < num_slots:
claimed.add(seg)
# Scan the valid range in order; anything not claimed is a gap.
# The ascending scan means no explicit sort is needed.
return [seg for seg in range(num_slots) if seg not in claimed]
A bytearray(num_slots) used as a boolean flag array is an equally correct alternative with a smaller per-element memory footprint and better cache locality. Both approaches have the same asymptotic complexity and produce identical outputs.