← Google Interview Insights

Google·Software Engineer·Onsite - Coding / Algorithms·Intermediate

IntermediatePrefer not to say
May 2026

Summary

Google SWE coding round with a graph connectivity problem that had a tricky follow-up. The base case was manageable but the unfriend extension is where things got interesting.

Questions Asked (2)

Q1

Given n people and a stream of timestamped friendship events (t, a, b), find the earliest time t at which all n people are in a single connected group. Return -1 if it never happens.

Algorithms & Data Structures
Author's notes

Pretty standard union-find setup once you see it.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as dynamic connectivity: process friendship events in chronological order, maintaining connected components. Use Union-Find (Disjoint Set Union) with union by rank and path compression to efficiently merge components, and track the number of components to detect when it becomes 1.

Pro tip: Mention that if the events are not sorted by time, you must sort them first (O(m log m)), and clarify whether the input is guaranteed sorted. Also, discuss the trade-off between Union-Find and other approaches like BFS/DFS for each time step, emphasizing Union-Find's near-constant time per operation.

1. Clarify input and constraints

Ask if the events are sorted by timestamp, the range of n and number of events, and whether multiple events can have the same timestamp. This affects sorting and tie-breaking.

2. Choose data structure

Select Union-Find (Disjoint Set Union) with path compression and union by rank/size for efficient merging and component counting.

3. Process events in order

Iterate through events sorted by time. For each event, union the two people if they are in different components, and decrement the component count accordingly.

4. Check for full connectivity

After each union, if the number of components becomes 1, return the current timestamp. If all events are processed without reaching 1, return -1.

5. Analyze complexity

State time complexity: O(m α(n)) for processing after sorting, where m is number of events and α is the inverse Ackermann function. Space: O(n).

Key Points to Mention

  • Union-Find (Disjoint Set Union) with path compression and union by rank/size
  • Tracking the number of connected components to detect when it reaches 1
  • Handling unsorted events by sorting them by timestamp first
  • Time complexity: O(m log m) if sorting needed, otherwise O(m α(n)); space O(n)
  • Edge cases: n=1 (already connected, return 0 or earliest time?), no events, events that don't connect all
  • Alternative approaches like BFS/DFS per timestamp are less efficient; explain why Union-Find is optimal

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

Q2

Follow-up: the event stream can now also include unfriend events. How do you handle deletions and still return the earliest time the graph becomes fully connected?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is where I kind of stalled.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify that deletions make the problem dynamic and that a simple union-find is insufficient because it doesn't support edge removal. Then propose a robust approach like maintaining a dynamic connectivity structure (e.g., Euler Tour Trees or Link-Cut Trees) or using offline processing with divide-and-conquer over time and rollback union-find to find the earliest time when the graph becomes connected.

Pro tip: Mention that if the graph is guaranteed to become connected and stay connected, you can binary search on time using a dynamic connectivity check, but be prepared to discuss the trade-offs of different data structures.

1. Clarify the problem

Confirm that unfriend events remove edges, and we need the earliest time when the graph is connected (all nodes in one component). Ask if the graph is guaranteed to become connected eventually and if nodes can be isolated.

2. Identify challenges

Explain that deletions break standard union-find, so we need a dynamic connectivity data structure or an offline algorithm that handles edge removals.

3. Propose a solution

Describe an offline approach: process events in reverse time (treating deletions as additions) and use union-find to find the earliest time the graph becomes connected, or use a dynamic connectivity structure like Euler Tour Trees for online queries.

4. Analyze trade-offs

Compare approaches: offline reverse processing is simpler and O((V+E) α(V)) but requires all events upfront; online dynamic connectivity is more complex but handles streaming. Discuss memory and time complexity.

5. Handle edge cases

Consider cases where the graph never becomes connected, multiple components, or when the earliest time is before any deletions. Also discuss how to track the number of connected components.

Key Points to Mention

  • Union-find does not support deletions, so we need a different approach.
  • Offline reverse processing: treat deletions as additions and use union-find to find the earliest time when the graph becomes connected.
  • Dynamic connectivity data structures: Euler Tour Trees or Link-Cut Trees for online queries.
  • Trade-offs: offline is simpler but requires all events; online is complex but handles streaming.
  • Track the number of connected components to know when the graph is fully connected.
  • Time complexity: offline O((V+E) α(V)), online O(log^2 V) per operation with Euler Tour Trees.

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