← Google Interview Insights

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

IntermediatePrefer not to say
May 2026

Summary

Google SWE coding round with a graph/union-find problem involving two-digit numbers grouped by shared digits. Not the hardest problem on paper but the transitive connectivity part is easy to miss if you're not thinking carefully.

Questions Asked (1)

Q1

Given an array of two-digit integers, group numbers together if they share at least one digit. The grouping is transitive, so if A shares a digit with B and B shares a digit with C, all three are in the same group. Return the size of the largest group.

Algorithms & Data Structures
Author's notes

My first instinct was to just do pairwise comparisons and call it a day.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as a graph where each number is a node and edges connect numbers sharing a digit, then find the largest connected component. Alternatively, use Union-Find (Disjoint Set Union) to efficiently merge groups based on shared digits, tracking the size of each component.

Pro tip: Since numbers are two-digit, you can optimize by using the digits themselves (0-9) as intermediate nodes in a bipartite graph, reducing the number of nodes to at most 10 + N and simplifying the union operations.

1. Clarify and Confirm

Restate the problem to ensure understanding: group two-digit integers transitively if they share at least one digit, and return the largest group size. Ask about edge cases like empty input, single number, or numbers with repeated digits (e.g., 11).

2. Choose Data Structure

Decide between graph traversal (BFS/DFS) or Union-Find. Union-Find is often more efficient for dynamic connectivity and easier to implement for transitive grouping.

3. Design Algorithm

For Union-Find: initialize each number as its own set. For each digit (0-9), keep a list of numbers containing that digit. Union all numbers in each digit's list. Then find the maximum set size. For graph: build adjacency list and run BFS/DFS to find largest component.

4. Analyze Complexity

Discuss time and space complexity. Union-Find with path compression and union by rank: O(N α(N)) time, O(N) space. Graph approach: O(N + E) time, where E is number of edges (at most 10*N).

5. Test and Validate

Walk through examples, including edge cases like [11, 22, 33] (all separate), [12, 23, 34] (all connected), and [10, 20, 30] (all connected via 0). Verify the algorithm returns correct sizes.

Key Points to Mention

  • Transitive grouping implies connected components in a graph.
  • Union-Find (Disjoint Set Union) with path compression and union by rank for efficiency.
  • Using digits as intermediate nodes to reduce edges (bipartite graph representation).
  • Time complexity: O(N α(N)) for Union-Find, which is nearly linear.
  • Space complexity: O(N) for storing parent and size arrays.
  • Handling edge cases: empty array, single element, numbers with same digits (e.g., 11).

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