← Google Interview Insights

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

Senior
Jun 2026

Summary

Google SWE coding round, one main problem with a follow-up that honestly separated the candidates who actually knew their stuff from those who just memorized Union-Find. The base case was fine, the follow-up was where things got interesting.

Questions Asked (2)

Q1

Design a data structure to support adding friendships between n people and answering connectivity queries (are two people in the same friend group?). Only additions, no deletions.

Algorithms & Data Structures
Author's notes

Pretty standard Union-Find territory.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Recognize this as a dynamic connectivity problem with only edge additions, which is ideally solved using a Union-Find (Disjoint Set Union) data structure. Explain the core operations (find and union) and how path compression and union by rank achieve near-constant time complexity. Then, discuss how to implement it efficiently and handle the given constraints.

Pro tip: Mention that Union-Find is optimal for incremental connectivity but would fail if deletions were allowed, showing you understand the problem's boundaries. Also, briefly note that the amortized time per operation is effectively O(1), which is crucial for large-scale systems.

1. Clarify the problem

Confirm that only additions occur and queries ask if two people are in the same connected component. Ask about constraints (e.g., number of people, number of operations) to guide implementation choices.

2. Choose the data structure

Propose Union-Find (Disjoint Set Union) as the ideal structure for incremental connectivity. Explain that it maintains disjoint sets and supports union and find operations efficiently.

3. Detail the implementation

Describe the parent array and optionally rank/size array. Explain find with path compression and union by rank/size, and how these optimizations lead to near-constant time per operation.

4. Analyze complexity

State that with both optimizations, the amortized time per operation is O(α(n)), where α is the inverse Ackermann function, effectively constant. Space complexity is O(n).

5. Discuss extensions and edge cases

Mention handling of duplicate friendships, self-friendships, and that deletions would require a different approach (e.g., dynamic connectivity with Euler tour trees).

Key Points to Mention

  • Union-Find (Disjoint Set Union) is the standard data structure for incremental connectivity.
  • Path compression and union by rank/size yield amortized O(α(n)) time per operation.
  • The find operation determines the representative (root) of a set; two people are connected if they have the same root.
  • The union operation merges two sets by attaching one root to another, using rank/size to keep trees shallow.
  • Initialization: each person starts as their own parent (singleton set).
  • Deletions are not supported efficiently; if needed, consider more complex structures like link-cut trees or Euler tour trees.

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

Q2

Now support edge deletions as well. How would you handle fully dynamic connectivity where friendships can both be added and removed?

Algorithms & Data StructuresTechnical Trade-offsSystem Design
Author's notes

This is where I kind of froze for a bit.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by acknowledging that fully dynamic connectivity is a hard problem with no simple optimal solution, then present a layered strategy: for small scale, use DFS/BFS with adjacency lists; for larger scale, discuss advanced data structures like Euler Tour Trees with balanced BSTs or Link-Cut Trees. Emphasize trade-offs between update and query times, and mention offline approaches like divide-and-conquer over time if queries are known in advance.

Pro tip: Mention that in practice, for many applications, a simpler approach like maintaining a spanning forest with periodic rebuilds or using union-find with rollback for offline queries is often sufficient, showing you understand real-world engineering constraints.

1. Clarify requirements and constraints

Ask about the expected scale (number of users, frequency of updates/queries), whether queries are online or offline, and if approximate or eventual consistency is acceptable.

2. Discuss naive and intermediate solutions

Propose simple approaches like BFS/DFS per query or maintaining a spanning forest with periodic rebuilds, and analyze their time complexities.

3. Introduce advanced data structures

Explain Euler Tour Trees (ETT) with balanced BSTs for dynamic trees, or Link-Cut Trees for path queries, and how they support edge insertions/deletions in O(log n) time.

4. Consider offline algorithms

If all operations are known in advance, describe divide-and-conquer over time with rollback union-find, which handles deletions efficiently.

5. Evaluate trade-offs and recommend

Compare solutions based on update/query time, implementation complexity, and memory, and suggest the most suitable for the given context.

Key Points to Mention

  • Fully dynamic connectivity requires handling both insertions and deletions, which is significantly harder than incremental connectivity.
  • Euler Tour Trees (ETT) maintain a spanning forest and support link/cut in O(log n) with balanced BSTs.
  • Link-Cut Trees support dynamic trees and path queries, often used for connectivity in dynamic graphs.
  • Offline divide-and-conquer over time with rollback union-find achieves O(log^2 n) per operation when all queries are known.
  • Trade-offs: online vs offline, exact vs approximate, and implementation complexity vs performance.
  • In practice, simpler heuristics like periodic rebuilds or maintaining a spanning forest with BFS may suffice for moderate scale.

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