KatInterview

Peak Active Couriers: Sweep-Line with Per-Worker Deduplication

Greedy & IntervalsModel answer
MediumModel answerGreedy & Intervalssweep-lineintervalsdeduplication

Last updated

Given delivery records with overlapping intervals per courier, find the maximum number of distinct couriers working simultaneously using a sweep-line algorithm.

Peak Active Couriers

A logistics platform tracks courier activity as a list of delivery records. Each record is a tuple (courier_id, start_time, end_time) where both times are integers and end_time is exclusive — a courier holding interval [s, e) is active at every moment t where s <= t < e.

A courier may accept several deliveries at once. That means one courier can appear in multiple overlapping records. For the purposes of this problem, a courier counts as one active courier regardless of how many deliveries they are simultaneously handling.

What you need to do

Part a) Write a function that returns the maximum number of distinct couriers active at any single moment in time.

Part b) Design and implement an O(n log n) sweep-line solution. As part of your answer:

  • Explain how you construct events from the input records.
  • Explain how you avoid counting the same courier twice when they hold multiple overlapping deliveries.
  • Explain how ties at the same timestamp should be broken so that a record ending at time t does not overlap a record starting at time t.

Part c) Suppose a teammate writes sorting key (courier_id, time, delta) for the event tuples. Explain precisely why this is wrong, give a concrete counter-example, and provide the correct sort key with justification.

Part d) How would the solution change if you instead wanted the peak number of concurrent deliveries (not distinct couriers)? What simplification becomes possible?

Function signature

def peak_active_couriers(records: list[tuple[int, int, int]]) -> int:
    ...

Examples

records = [(1, 1, 5), (2, 2, 6)]
# Courier 1 active [1,5), Courier 2 active [2,6)
# During [2,5) both are active → peak = 2
peak_active_couriers(records) == 2

records = [(1, 1, 5), (1, 3, 8)]
# Same courier holds two overlapping deliveries
# Only 1 distinct courier at any moment → peak = 1
peak_active_couriers(records) == 1

records = [(1, 1, 3), (2, 3, 5)]
# Record 1 ends at 3, record 2 starts at 3 — exclusive end means no overlap
peak_active_couriers(records) == 1

Constraints

  • 0 <= len(records) <= 10^5
  • All times are integers (may be negative or very large; only relative order matters).
  • Records with start_time >= end_time represent empty intervals and should be ignored.
  • Your solution must run in O(n log n) time and O(n) space.

What a strong answer covers

  • Correct deduplication logic and why a simple boolean flag fails.
  • The exact sort key and why primary sorting on anything other than time breaks the sweep.
  • Complexity analysis for both the deduplicated and non-deduplicated variants.
  • Edge cases: empty input, zero-length intervals, a single courier with many nested deliveries, back-to-back intervals for the same courier.

Hints

Hint 1

Turn each record into two timestamped events: one for when the courier becomes (potentially) active and one for when they become (potentially) inactive. Then process events in time order — this is the core sweep-line idea.

Hint 2

A boolean 'is this courier active?' flag breaks when a courier holds nested or overlapping deliveries. Think about what additional per-courier state lets you correctly delay the 'courier goes idle' transition until all their open deliveries are closed.

Hint 3

When two events share the same timestamp, the type of event matters for correctness. Ask yourself: should a delivery ending at time t and one starting at time t be considered overlapping? Use that answer to decide which event type must be processed first, and encode that ordering into your sort key.