Earliest Time of Full Connectivity
GoogleSoftware Engineer · OnsiteLast updated
Determine the earliest timestamp at which all nodes in a network become part of a single connected component using a chronological sequence of edge additions.
Problem Statement
You are given an integer n representing the number of nodes in a network, labeled from 0 to n - 1. Initially, each node is isolated with no connections.
You are also given a list of connection events events, where each event is represented as a list of three integers [timestamp, u, v]. This indicates that at time timestamp, a bidirectional connection was formed between node u and node v.
Return the earliest timestamp at which the entire network becomes fully connected (i.e., every node can reach every other node through some sequence of connections). If the network never becomes fully connected, return -1.
Function Signature
def earliest_connectivity_time(n: int, events: list[list[int]]) -> int:
pass
Constraints
2 <= n <= 10^51 <= len(events) <= 2 * 10^5events[i] = [timestamp, u, v]0 <= timestamp <= 10^90 <= u, v < nandu != v- The events are not guaranteed to be sorted by
timestamp. - Multiple edges between the same pair of nodes can occur at the same or different timestamps.
Examples
Example 1
Input: n = 4, events = [[20230101, 0, 1], [20230104, 2, 3], [20230102, 1, 2], [20230103, 0, 2]]
Output: 20230104
Explanation:
- Time 20230101: Node 0 and Node 1 connect. Components: {0, 1}, {2}, {3}.
- Time 20230102: Node 1 and Node 2 connect. Components: {0, 1, 2}, {3}.
- Time 20230103: Node 0 and Node 2 connect. They are already in the same component, so component count does not change.
- Time 20230104: Node 2 and Node 3 connect. All nodes {0, 1, 2, 3} are now connected. The earliest time is 20230104.
Example 2
Input: n = 4, events = [[5, 0, 1], [10, 1, 2]]
Output: -1
Explanation: Node 3 is never connected to any other node, so the graph is never fully connected.
Edge Cases
- Redundant edges that connect nodes already in the same component should not decrease the component count.
- Events with identical timestamps.
- Insufficient events to ever connect all nodes (
len(events) < n - 1).
Hints
Hint 1
Since connectivity is cumulative over time, processing the edge events in chronological order is necessary.
Hint 2
Keep track of the number of distinct connected components, starting with n components and decrementing whenever two previously disjoint components merge.
Hint 3
A Disjoint Set Union (DSU) data structure with path compression and union-by-rank allows you to perform find and merge operations in near-constant time.
Follow-up questions
- How would you adapt your approach if edge events arrive out of order in a streaming environment?
- What changes would be required if connections could also expire or be removed at specific timestamps?
- If n is on the order of billions and cannot fit in memory on a single machine, how would you design a distributed connectivity-tracking system?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.