Earliest Timestamp When Everyone in a Group Chat Is Connected
Last updated: November 29, 2026
Quick Overview
Given n members of a Messenger community and a log of (timestamp, a, b) events meaning a and b became friends at that time, return the earliest timestamp after which every member is connected through friendships, or -1 if that never happens.
Meta
November 29, 20260
0
0 solved
Given n members of a Messenger community and a log of (timestamp, a, b) events meaning a and b became friends at that time, return the earliest timestamp after which every member is connected through friendships, or -1 if that never happens.
A Meta phone screen style question built on the social graph: tracking when a community becomes one connected cluster. The interviewer checks that you sort the log by time, union as you go, and stop the moment the component count reaches one.
What the Interviewer Expects
- Sort events by timestamp before processing
- Maintain a component count that drops on each successful union
- Return the current timestamp when the count hits one
- Return -1 if the log ends first
- State O(m log m) time
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you answer 'when did a and b first become connected' for many pairs?
- What if friendships can also be removed later in the log?
- How does this relate to Kruskal's algorithm?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input:
logsas[timestamp, a, b](possibly unsorted), andnmembers0..n-1. - Output: earliest timestamp when all
nare in one component, else-1....
High-Level Design
Connectivity only grows as friendships are added, so I can replay events in time order with a union find and a counter components starting at n. Each union that joins two different roots decrement...