← Google Interview Insights

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

IntermediatePrefer not to say
Jun 2026

Summary

Google SWE coding round, graph traversal problem with BFS. Pretty standard but the string-keyed nodes tripped me up a bit on implementation.

Questions Asked (1)

Q1

Given a list of undirected relationships between named entities and a set of queries, determine for each query (s, t) whether s can reach t by following relationship edges.

Algorithms & Data Structures
Author's notes

I knew it was BFS or union-find right away, but the string identifiers instead of integer node IDs slowed me down.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the entities and relationships as an undirected graph, then preprocess it to answer connectivity queries efficiently. Use Union-Find (Disjoint Set Union) to group connected components, allowing each query to be answered in nearly constant time. Alternatively, if the graph is static, you can run BFS/DFS from each query source, but that is less efficient for many queries.

Pro tip: Always clarify constraints first: number of entities, edges, and queries. If queries are numerous, Union-Find is optimal; if edges are added dynamically, Union-Find with path compression and union by rank handles it well. Mention that for very large graphs, you might need to consider memory and use iterative DFS to avoid stack overflow.

1. Clarify the problem and constraints

Ask about the size of the input (number of entities, edges, queries), whether the graph is static or dynamic, and if there are any memory or time limits. This determines the best algorithm.

2. Choose the right data structure

For static graphs with many queries, Union-Find is ideal. For dynamic graphs with edge additions, Union-Find also works. If edges can be removed, more complex structures like Link-Cut Trees might be needed, but that's rare.

3. Preprocess the graph

Build the Union-Find structure by iterating through all edges and unioning the endpoints. This groups all connected entities into disjoint sets.

4. Answer queries

For each query (s, t), check if s and t belong to the same set using find operations. If they do, they are connected; otherwise, they are not.

5. Analyze complexity and optimize

With path compression and union by rank, preprocessing takes O(E α(N)) and each query O(α(N)), where α is the inverse Ackermann function. This is effectively constant time.

Key Points to Mention

  • Graph representation: adjacency list vs. edge list
  • Union-Find (Disjoint Set Union) with path compression and union by rank
  • Time complexity: O((E + Q) α(N)) for Union-Find approach
  • Alternative approaches: BFS/DFS per query (O(Q*(V+E))) or precomputing connected components
  • Handling large inputs: iterative DFS to avoid recursion depth issues, memory considerations
  • Edge cases: self-loops, duplicate edges, disconnected entities, queries with same source and target

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