← Google Interview Insights

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

Intermediate
May 2026

Summary

Google SWE coding round, one algorithmic problem about grouping numbers by shared digits. Pretty clean problem on the surface but the edge cases tripped me up a bit.

Questions Asked (1)

Q1

Given a list of two-digit numbers, find the maximum subset where every element shares at least one digit with at least one other element in the subset.

Algorithms & Data Structures
Author's notes

My first instinct was union-find but I second-guessed myself and started rambling about graphs.

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. The maximum subset is the largest connected component. Use union-find or BFS/DFS to find it efficiently.

Pro tip: Clarify whether the subset must be connected (i.e., every element shares a digit with at least one other in the subset) or just that no element is isolated. The former is the standard interpretation and leads to connected components.

1. Clarify the problem

Confirm that the subset must be connected: every element shares at least one digit with at least one other element in the subset. Also confirm if numbers are two-digit (10-99) and if duplicates are allowed.

2. Model as a graph

Treat each number as a node. Add an edge between two numbers if they share at least one digit (tens or ones). The problem reduces to finding the largest connected component.

3. Choose an algorithm

Use union-find (disjoint set) or BFS/DFS to find connected components. Union-find is efficient for large inputs; BFS/DFS is simpler to implement.

4. Optimize with digit mapping

Instead of checking all pairs, map each digit (0-9) to the list of numbers containing it. Then union all numbers sharing a digit. This reduces time complexity.

5. Analyze complexity and edge cases

Time: O(N * α(N)) with union-find and digit mapping, where N is the number of elements. Space: O(N). Handle edge cases: empty list, single element, no shared digits.

Key Points to Mention

  • Graph representation: nodes as numbers, edges for shared digits.
  • Connected components: the largest component is the answer.
  • Union-find (disjoint set) for efficient merging.
  • Digit mapping optimization: group numbers by each digit they contain.
  • Time and space complexity analysis.
  • Edge cases: empty input, single element, no connections.

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