← Google Interview Insights

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

Intermediate
May 2026

Summary

Google SWE coding round, graph problem that looks simple on the surface but has a few ways to approach it. Nothing too wild but the union-find angle is easy to miss if you default straight to BFS.

Questions Asked (1)

Q1

Given an n x n adjacency matrix representing direct friendships, return the total number of friend circles (connected components), keeping in mind that friendship is transitive.

Algorithms & Data Structures
Author's notes

My first instinct was BFS and it works fine, but I think they were nudging toward union-find.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as finding connected components in an undirected graph where each person is a node and friendships are edges. Use either DFS/BFS to traverse each component or Union-Find to merge connected people, counting the number of components. Discuss trade-offs between the two approaches based on graph density and constraints.

Pro tip: Mention that the adjacency matrix is symmetric and represents an undirected graph, so you can optimize by only traversing the upper triangle or using Union-Find with path compression and union by rank for near-linear time. Also, clarify that the matrix is 1-indexed or 0-indexed and handle edge cases like n=0.

1. Clarify the problem and constraints

Confirm that the matrix represents an undirected graph, friendship is transitive, and ask about input size to choose the right algorithm. Clarify if the matrix is guaranteed symmetric and if self-friendship (diagonal) is always 1.

2. Choose an algorithm

Decide between DFS/BFS (simpler, O(n^2) time, O(n) space) and Union-Find (efficient for dynamic connectivity, O(n^2 α(n)) time, O(n) space). Explain your choice based on constraints.

3. Implement the solution

For DFS/BFS: iterate through each person, if unvisited, increment circle count and traverse all connected friends. For Union-Find: initialize each person as its own parent, union all friends, then count unique roots.

4. Analyze complexity and edge cases

State time and space complexity. Discuss edge cases: n=0, n=1, all friends (one circle), no friends (n circles), and disconnected groups.

5. Test with examples

Walk through a small example (e.g., 3x3 matrix) to verify correctness. If time permits, mention potential optimizations like early termination or using bitsets for dense graphs.

Key Points to Mention

  • Graph representation: adjacency matrix as an undirected graph
  • Connected components: definition and how it maps to friend circles
  • DFS/BFS traversal: marking visited nodes and counting components
  • Union-Find (Disjoint Set Union): path compression and union by rank optimizations
  • Time and space complexity: O(n^2) time, O(n) space for both approaches
  • Edge cases: n=0, n=1, fully connected, completely disconnected

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