← Google Interview Insights

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

SeniorPrefer not to say
Jul 2026

Summary

Google onsite coding round for SWE, centered almost entirely on a single graph connectivity problem that kept escalating. The base case felt manageable but the follow-ups got genuinely hard fast.

Questions Asked (3)

Q1

Given a chronologically ordered log of friend events between N people, find the earliest timestamp at which all N people are connected (i.e. everyone is a transitive friend).

Algorithms & Data Structures
Author's notes

This part I actually felt okay about.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as building a graph incrementally over time and detecting when it becomes connected. Use a Union-Find (Disjoint Set Union) data structure to efficiently track connected components as edges are added in chronological order, stopping when the number of components reaches 1. The timestamp of the edge that causes the final merge is the earliest time all N people are connected.

Pro tip: Mention that you can optimize by early termination and that Union-Find with path compression and union by rank gives near-constant time per operation, making the solution O(E α(N)) which is optimal. Also, clarify edge cases like N=1 (already connected at time 0) and disconnected graphs (return -1 or null).

1. Clarify the problem and constraints

Confirm that events are edges between two people with timestamps, and that 'connected' means the graph is connected (one component). Ask about input size, whether timestamps are unique, and what to return if never connected.

2. Choose the right data structure

Select Union-Find (Disjoint Set Union) to dynamically maintain connected components as edges are added. Explain why it's better than BFS/DFS per timestamp (which would be O(E*(N+E))).

3. Process events chronologically

Initialize each person as a separate component. Iterate through events in order, union the two people, and after each union check if the number of components has decreased to 1. If so, return the current timestamp.

4. Handle edge cases and return value

If N=1, return 0 (or the earliest time). If the loop finishes without all connected, return -1 or null. Also consider if multiple events share the same timestamp—process all before checking connectivity.

5. Analyze complexity and test

State time complexity O(E α(N)) and space O(N). Walk through a small example to verify correctness, and discuss potential optimizations like early exit.

Key Points to Mention

  • Union-Find (Disjoint Set Union) with path compression and union by rank for near-constant time operations.
  • Incremental graph connectivity: adding edges one by one and tracking component count.
  • Early termination when component count reaches 1, avoiding processing remaining events.
  • Time complexity: O(E α(N)) where α is the inverse Ackermann function, effectively linear.
  • Edge cases: N=1, no events, disconnected graph, multiple events with same timestamp.
  • Alternative approaches: binary search on timestamp with BFS/DFS, but less efficient than Union-Find.

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

Q2

Now answer pairwise connectivity queries at arbitrary timestamps: given a time T and two people X and Y, were they connected at time T? The event log may include both friend and unfriend actions.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is where things fell apart a bit.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the event log as a dynamic graph where edges are added (friend) and removed (unfriend) over time. Preprocess the log into a data structure that supports connectivity queries at arbitrary timestamps, such as a segment tree over time with rollback DSU or a temporal graph index. Then answer each query by checking if X and Y are in the same connected component at time T.

Pro tip: Discuss the trade-off between preprocessing time/space and query time, and mention that for Google-scale data, an offline approach with segment tree + rollback DSU is often preferred over online methods like link-cut trees due to simplicity and efficiency.

1. Clarify requirements and constraints

Ask about the volume of events and queries, whether queries are online or offline, and if timestamps are discrete or continuous. This determines the appropriate data structure.

2. Choose a representation

Decide between offline (e.g., segment tree over time with rollback DSU) or online (e.g., link-cut trees, dynamic connectivity) approaches. Consider space-time trade-offs.

3. Preprocess the event log

Build the chosen data structure. For offline, assign each edge's active interval and insert into segment tree; for online, maintain a dynamic connectivity structure.

4. Answer queries

For each query (T, X, Y), traverse the data structure to determine connectivity at time T. For offline, process queries in time order with rollback; for online, query the structure directly.

5. Analyze complexity and optimize

Discuss time and space complexity, and potential optimizations like compression, batching, or caching frequent queries.

Key Points to Mention

  • Dynamic connectivity problem and its variants (fully dynamic, incremental, decremental)
  • Segment tree over time with rollback DSU for offline queries
  • Link-cut trees or Euler tour trees for online queries
  • Trade-offs: preprocessing time vs query time, memory usage, implementation complexity
  • Handling unfriend actions (edge deletions) and ensuring correctness
  • Scalability considerations for large-scale systems (e.g., distributed processing, approximation)

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

Q3

How would you handle fully dynamic connectivity, where unfriend events can happen at any time and queries arrive online (not in a batch)?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I basically said 'link-cut trees or Holm-Lichtenberg, polylog per operation, genuinely hard to implement in an interview.' Then I offered the brute-force fallback: rebuild the graph from scratch per query with BFS.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem constraints (number of vertices/edges, query types, memory limits) and then explain that fully dynamic connectivity with deletions is hard: no known polylogarithmic update/query solution exists for general graphs. Propose practical approaches like Euler Tour Trees for forests, or randomized/amortized structures (e.g., Holm-de Lichtenberg-Thorup) for general graphs, and discuss trade-offs between update and query time.

Pro tip: Acknowledge that Google interviewers value depth over buzzwords: mention that for forests, Euler Tour Trees give O(log n) updates and queries, but for general graphs, the best known is O(log^2 n) amortized update and O(log n / log log n) query. Also note that if deletions are rare, offline or batch processing might be acceptable, but since queries are online, you need a dynamic structure.

1. Clarify constraints and requirements

Ask about the number of vertices and edges, frequency of updates vs. queries, memory limits, and whether approximate answers are acceptable. This determines whether a simple solution (e.g., BFS per query) is viable or a sophisticated dynamic structure is needed.

2. Identify the core difficulty

Explain that dynamic connectivity with deletions is fundamentally harder than incremental connectivity (only additions). For general graphs, no polylogarithmic worst-case solution is known; the best is randomized/amortized.

3. Propose a solution for forests (Euler Tour Trees)

If the graph is a forest, use Euler Tour Trees (ETT) to support link, cut, and connectivity in O(log n) time. This is a common building block and demonstrates knowledge of dynamic trees.

4. Extend to general graphs (Holm-de Lichtenberg-Thorup)

For general graphs, describe the Holm-de Lichtenberg-Thorup (HDT) algorithm: maintain a spanning forest and use ETT to support it, with O(log^2 n) amortized update and O(log n / log log n) query. Mention that this is complex to implement.

5. Discuss trade-offs and alternatives

Compare with simpler approaches like periodic rebuilding or using a union-find with rollback for offline, but note they don't fit online queries. Also mention that if the graph is dense, a different approach might be better.

Key Points to Mention

  • Euler Tour Trees for dynamic forests: O(log n) link, cut, and connectivity.
  • Holm-de Lichtenberg-Thorup algorithm for general graphs: O(log^2 n) amortized update, O(log n / log log n) query.
  • The distinction between incremental (only additions) and fully dynamic (additions and deletions) connectivity.
  • The trade-off between update and query time; no known polylogarithmic worst-case solution for general graphs.
  • Practical considerations: implementation complexity, memory overhead, and whether the problem allows for approximations or offline processing.
  • Alternative: if deletions are rare, use a dynamic connectivity structure with periodic rebuilding or a union-find with rollback for offline, but note online queries require a fully dynamic structure.

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