Earliest Time of Full Connectivity

Quick Overview

Find the earliest event timestamp at which an evolving undirected graph becomes fully connected.

Earliest Time of Full Connectivity

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

# Earliest Time of Full Connectivity Implement `earliest_full_connection(n: int, events: list[list[int]]) -> int`. There are `n` entities labeled `0` through `n - 1`. Each event is `[timestamp, a, b]` and creates a permanent undirected connection between entities `a` and `b`. Events may be given in any order. Return the earliest timestamp at which all entities are connected through direct or indirect connections. ### Input Domain - `1 <= n <= 200,000`. - `0 <= len(events) <= 300,000`. - Timestamps fit in signed 64-bit integers. - Every endpoint is a valid entity label; self-connections and repeated connections may occur. ### Output Rules - All events with the same timestamp take effect as one time group. - Return the timestamp of the first group after which there is exactly one connected component. - Return `0` when `n = 1`, even if there are no events. - Return `-1` if full connectivity never occurs. ### Constraints - Ordering among events with equal timestamps must not change the answer. - Target time is `O(m log m + m alpha(n))` for `m` events. ### Examples #### Example 1 Input: `n = 4, events = [[5,0,1],[2,2,3],[7,1,2]]` Output: `7` #### Example 2 Input: `n = 3, events = [[4,0,1],[4,1,0]]` Output: `-1` ```hint Track component count After sorting by timestamp, a successful union reduces the number of connected components; full connectivity occurs when that count reaches one. ```

Quick Answer: Find the earliest event timestamp at which an evolving undirected graph becomes fully connected.

|Home/Coding & Algorithms/Google
Google logo
Google
Aug 18, 2026
mediumSoftware EngineerOnsiteCoding & Algorithms
6
0

Earliest Time of Full Connectivity

Implement earliest_full_connection(n: int, events: list[list[int]]) -> int.

There are n entities labeled 0 through n - 1. Each event is [timestamp, a, b] and creates a permanent undirected connection between entities a and b. Events may be given in any order. Return the earliest timestamp at which all entities are connected through direct or indirect connections.

Input Domain

  • 1 <= n <= 200,000 .
  • 0 <= len(events) <= 300,000 .
  • Timestamps fit in signed 64-bit integers.
  • Every endpoint is a valid entity label; self-connections and repeated connections may occur.

Output Rules

  • All events with the same timestamp take effect as one time group.
  • Return the timestamp of the first group after which there is exactly one connected component.
  • Return 0 when n = 1 , even if there are no events.
  • Return -1 if full connectivity never occurs.

Constraints

  • Ordering among events with equal timestamps must not change the answer.
  • Target time is O(m log m + m alpha(n)) for m events.

Examples

Example 1

Input: n = 4, events = [[5,0,1],[2,2,3],[7,1,2]]

Output: 7

Example 2

Input: n = 3, events = [[4,0,1],[4,1,0]]

Output: -1

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...