KatInterview

Earliest Moment a Social Network Becomes Fully Connected

Trees & GraphsAuto-graded · 20 test cases
HardAuto-gradedTrees & Graphsunion-findsegment-treedynamic-connectivity

Last updated

Tests offline dynamic graph connectivity using a segment-tree-over-time with a rollback-capable union-find to handle both edge insertions and deletions.

Problem

You are given a chronological log of interactions between people in a social network. Each entry is a list of four values:

[timestamp, person_A, event_type, person_B]

There are two event types:

  • "shared-ride-with" — person_A and person_B formed a direct connection at the given timestamp.
  • "blocked" — starting at the given timestamp, the direct connection between person_A and person_B is severed (even if they connected earlier).

Two people are reachable from each other if a chain of active direct connections links them. Reachability is transitive: if A is connected to B and B is connected to C, then A can reach C.

Your task is to find the earliest timestamp at which every person who appears anywhere in the log belongs to a single connected group. If this never happens, return -1.


Rules and Edge Cases

  • Timestamps are given as integers (already parsed; no string conversion needed).
  • Events that share the same timestamp are applied in the order they appear in the input list. Connectivity is checked after each individual event.
  • A "shared-ride-with" event between two people who are already directly connected does not reset or duplicate their connection interval.
  • A "blocked" event between two people who are not currently directly connected is a no-op.
  • A person can reconnect with someone they previously blocked; the new connection starts a fresh interval from that event onward.
  • A "shared-ride-with" event where both names are the same adds no connection. A log containing only one distinct person is considered fully connected from the first event's timestamp.
  • Every person who appears in any event — including those who appear only in "blocked" events — counts toward the group that must become fully connected.
  • If the log is empty, return -1.
  • If any event does not have exactly four fields, or contains an unrecognized event type, raise a ValueError.
  • Connections can come and go. If the network was fully connected at some earlier moment but later split due to a block, the answer is still that earlier timestamp.

Constraints

  • 0 ≤ len(events) ≤ 10^4
  • Each event has the form [timestamp, name_A, event_type, name_B]
  • Timestamps are non-negative integers
  • Names are non-empty strings
  • event_type is either "shared-ride-with" or "blocked"; any other value raises ValueError
  • An event with fewer or more than 4 fields raises ValueError

Examples

Example 1

Input: earliest_all_connected_time([[1, 'Alice', 'shared-ride-with', 'Bob'], [2, 'Charlie', 'shared-ride-with', 'Dan'], [3, 'Bob', 'shared-ride-with', 'Charlie'], [4, 'Alice', 'shared-ride-with', 'Eve'], [5, 'Bob', 'shared-ride-with', 'Dan']])

Output: 4

Example 2

Input: earliest_all_connected_time([[1, 'A', 'shared-ride-with', 'B'], [2, 'C', 'shared-ride-with', 'D']])

Output: -1

Hints

Hint 1

For the add-only version (no blocks), a plain union-find processed in time order is enough — each merge reduces the component count by one, and you stop the moment it hits 1.

Hint 2

Once block events are introduced, edges have lifetimes: an edge is active over a contiguous interval of sorted-event slots, from when it is created to when it is blocked. Try representing each edge as one or more half-open slot intervals [start, end).

Hint 3

Build a segment tree whose leaves correspond to sorted event slots and insert each interval into O(log n) canonical nodes. Then DFS the tree left-to-right with a union-find that supports undo (union-by-size, no path compression), rolling back on exit from each node. The first leaf where the component count reaches 1 gives the answer.