Find the earliest timestamp at which every person in a group is acquainted

Quick Overview

Given n people and a list of timestamped friendship events, find the earliest time at which everyone is connected to everyone else through chains of friends, or -1 if that never happens. It tests modeling acquaintance as graph connectivity and keeping track of merging groups efficiently as edges arrive over time.

Find the earliest timestamp at which every person in a group is acquainted

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

There are `n` people in a social group labeled from `0` to `n - 1`. You are given an array `logs` where `logs[i] = [timestamp_i, x_i, y_i]` indicates that `x_i` and `y_i` become friends at the time `timestamp_i`. Friendship is symmetric: if `a` is friends with `b`, then `b` is friends with `a`. Also, person `a` is acquainted with person `b` if `a` is friends with `b`, or `a` is a friend of someone acquainted with `b`. Return the earliest time for which every person became acquainted with every other person. If there is no such time, return `-1`. ### Function Signature ```python def earliest_acquaintance_time(logs: list[list[int]], n: int) -> int: ``` ### Rules - `logs` is not necessarily sorted by timestamp. - A friendship, once formed, is never removed. - Return the timestamp of the log entry after which all `n` people are acquainted with each other, or `-1` if that is still not true after every entry. Timestamps are unique, so the answer is unique. ### Constraints - `2 <= n <= 10^5` - `1 <= logs.length <= 10^4` - `logs[i].length == 3` - `0 <= timestamp_i <= 10^9` - `0 <= x_i, y_i <= n - 1` - `x_i != y_i` - All the values `timestamp_i` are unique. - Each pair `(x_i, y_i)` occurs at most once in the input. ### Examples ```text Input: logs = [[20190101,0,1],[20190104,3,4],[20190107,2,3],[20190211,1,5],[20190224,2,4],[20190301,0,3],[20190312,1,2],[20190322,4,5]], n = 6 Output: 20190301 ``` By 20190211 the people form two groups, {0, 1, 5} and {2, 3, 4}; the 20190224 entry links two people who are already in the same group. At 20190301, persons 0 and 3 become friends, which merges the two groups, so from that point everyone is acquainted. ```text Input: logs = [[0,2,0],[1,0,1],[3,0,3]], n = 4 Output: 3 ``` After (2, 0) the group is {0, 2}, after (0, 1) it is {0, 1, 2}, and after (0, 3) it is {0, 1, 2, 3}. All four people are connected at timestamp 3. ```text Input: logs = [[7,0,1],[3,1,2],[9,0,2]], n = 3 Output: 7 ``` In time order, persons 1 and 2 become friends at 3 and persons 0 and 1 at 7, which connects all three people. The entry at 9 links two people who are already acquainted.

Overview: Given n people and a list of timestamped friendship events, find the earliest time at which everyone is connected to everyone else through chains of friends, or -1 if that never happens. It tests modeling acquaintance as graph connectivity and keeping track of merging groups efficiently as edges arrive over time.

PracHub logo
General
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

There are n people in a social group labeled from 0 to n - 1. You are given an array logs where logs[i] = [timestamp_i, x_i, y_i] indicates that x_i and y_i become friends at the time timestamp_i.

Friendship is symmetric: if a is friends with b, then b is friends with a. Also, person a is acquainted with person b if a is friends with b, or a is a friend of someone acquainted with b.

Return the earliest time for which every person became acquainted with every other person. If there is no such time, return -1.

Function Signature

def earliest_acquaintance_time(logs: list[list[int]], n: int) -> int:

Rules

  • logs is not necessarily sorted by timestamp.
  • A friendship, once formed, is never removed.
  • Return the timestamp of the log entry after which all n people are acquainted with each other, or -1 if that is still not true after every entry. Timestamps are unique, so the answer is unique.

Constraints

  • 2 <= n <= 10^5
  • 1 <= logs.length <= 10^4
  • logs[i].length == 3
  • 0 <= timestamp_i <= 10^9
  • 0 <= x_i, y_i <= n - 1
  • x_i != y_i
  • All the values timestamp_i are unique.
  • Each pair (x_i, y_i) occurs at most once in the input.

Examples

Input: logs = [[20190101,0,1],[20190104,3,4],[20190107,2,3],[20190211,1,5],[20190224,2,4],[20190301,0,3],[20190312,1,2],[20190322,4,5]], n = 6
Output: 20190301

By 20190211 the people form two groups, {0, 1, 5} and {2, 3, 4}; the 20190224 entry links two people who are already in the same group. At 20190301, persons 0 and 3 become friends, which merges the two groups, so from that point everyone is acquainted.

Input: logs = [[0,2,0],[1,0,1],[3,0,3]], n = 4
Output: 3

After (2, 0) the group is {0, 2}, after (0, 1) it is {0, 1, 2}, and after (0, 3) it is {0, 1, 2, 3}. All four people are connected at timestamp 3.

Input: logs = [[7,0,1],[3,1,2],[9,0,2]], n = 3
Output: 7

In time order, persons 1 and 2 become friends at 3 and persons 0 and 1 at 7, which connects all three people. The entry at 9 links two people who are already acquainted.