← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Google SWE interview with a graph/connectivity problem that's pretty standard if you've seen Union-Find before, but the details can trip you up under pressure.

Questions Asked (1)

Q1

Given a list of timestamped friendship events between n people, find the earliest timestamp at which everyone in the group is connected (directly or transitively). Return -1 if it never happens.

Algorithms & Data Structures
Author's notes

The transitive part is what makes this click as a Union-Find problem.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Sort the friendship events by timestamp and process them in chronological order using a Union-Find (Disjoint Set Union) data structure. After each union, check if the number of connected components has reduced to 1; if so, return the current timestamp. If all events are processed without full connectivity, return -1.

Pro tip: Mention that you can optimize by tracking the number of components and early-exiting when it reaches 1, and discuss the trade-offs between sorting upfront versus using a min-heap for streaming data.

1. Clarify the problem and edge cases

Confirm that the graph is undirected, timestamps may be unsorted, and multiple events can occur at the same timestamp. Discuss edge cases like n=1 (already connected) or no events.

2. Choose the right data structure

Select Union-Find with path compression and union by rank for near-constant time operations. Alternatively, consider BFS/DFS after sorting, but explain why Union-Find is more efficient for incremental connectivity.

3. Process events in chronological order

Sort the events by timestamp. Initialize Union-Find with n components. For each event, union the two people and decrement the component count if they were in different sets.

4. Check for full connectivity

After each union, if the component count becomes 1, return the current timestamp immediately. If the loop finishes without reaching 1, return -1.

5. Analyze complexity and test

State time complexity: O(m log m) for sorting plus O(m α(n)) for unions, where m is number of events. Space: O(n). Walk through a small example to verify correctness.

Key Points to Mention

  • Union-Find (Disjoint Set Union) with path compression and union by rank
  • Sorting events by timestamp to process in order
  • Tracking the number of connected components to detect full connectivity
  • Time complexity: O(m log m) due to sorting, space O(n)
  • Handling edge cases: n=1, no events, disconnected graph
  • Alternative approaches like BFS/DFS and their trade-offs

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.