2dbi
Home/Uber/Find earliest timestamp when all riders become connected
UUber·DSASDE-2Onsite: Coding 1

Find earliest timestamp when all riders become connected

Given a chronological log of ride-share events (each event connects two riders at a timestamp), find the earliest timestamp at which all riders form a single connected component.

Core approach: Union-Find (DSU) with union-by-rank + path compression. Replay events in timestamp order, union the two riders, and check after each union whether the number of components has reached 1. Time complexity: O(α(N) · E) where E is the number of events.

Common variations:

  • Events include block actions that sever connections — DSU doesn't support deletions, so fall back to offline BFS/DFS by replaying all events, or use a "link-cut tree" for dynamic connectivity.
  • Very large timestamps (12-digit) — store in long, no algorithmic impact but easy to overlook in parsing.

Key trade-offs to discuss: DSU is optimal for the additive-only case; dynamic disconnection requires significantly more complex data structures or a reverse-time (rollback DSU) trick.

added 2 days ago
ランキング給与
言語
アカウント