Find largest group of two-digit numbers sharing digits
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Take-home Project
Quick Answer: This question evaluates a candidate's ability to model pairwise relationships and compute connected components in small graphs, testing skills in graph connectivity and grouping based on shared attributes in arrays of two-digit numbers.
Constraints
- 1 <= n <= 100
- Each A[i] is a two-digit number (10 <= A[i] <= 99)
- Duplicate values are allowed and counted as separate elements
- Connection is transitive (group = connected component)
Examples
Input: ([55, 58, 25, 45],)
Expected Output: 4
Explanation: 55-58 share 5, 25-45 share 5, and 55-25/55-45 share 5, so all four numbers are in one component of size 4.
Input: ([55, 66, 77],)
Expected Output: 1
Explanation: 55, 66, 77 share no digit with each other, so every number is its own component; the largest size is 1.
Input: ([12, 23, 34, 99],)
Expected Output: 3
Explanation: 12-23 share 2, 23-34 share 3, so 12/23/34 form one transitive component of size 3; 99 is isolated.
Input: ([10],)
Expected Output: 1
Explanation: A single element forms a component of size 1.
Input: ([55, 55, 66],)
Expected Output: 2
Explanation: The two 55s are distinct elements that share digit 5, forming a component of size 2; 66 is isolated.
Input: ([90, 81, 72, 63],)
Expected Output: 1
Explanation: Digit sets {9,0},{8,1},{7,2},{6,3} are pairwise disjoint, so each number is its own component of size 1.
Hints
- Model each number as a node; add an edge between any two numbers that share a digit. The answer is the size of the largest connected component.
- A two-digit number has only its tens and ones digit, so 'shares a digit' is just a non-empty intersection of two 2-element digit sets — an O(1) check.
- Union-Find (Disjoint Set Union) makes the component sizes easy: union every connected pair, then count how many indices map to each root and take the max. A BFS/DFS over an adjacency list works too.
- Watch the duplicate clarification: identical values still count as distinct elements, so they each add 1 to the component — union by index, not by value.