← Questions
Largest group of two-digit numbers that share digits
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Google's HackerRank assessment, last asked Dec 2025. It tests graphs and union-find.
You get an array of two-digit integers (10 to 99). A group of them is valid when at least one digit appears in every number of the group: 52, 25, 55 is valid (they all contain 5); 11, 52, 34 is not. Return the size of the largest valid group you can pick. A single number is always a valid group.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
largest_group(nums: list[int]) -> int— the maximum group size;0for an empty array. Duplicate values count separately.
Example
largest_group([52, 25, 55]) -> 3 # all contain 5
largest_group([11, 52, 34]) -> 1 # no digit is shared by two of them
largest_group([10, 20, 30, 41, 11]) -> 3 # 10, 20, 30 share 0 (10, 41, 11 share 1 too)
Constraints
-
0 <= n, large enough that the solution must be linear. - Every value is between 10 and 99 inclusive.
Clarifying questions
- Must one digit be common to all members, or is it enough that neighbours share a digit?
- Does
55count once or twice for digit 5? - Do duplicates count as separate members?
- What should an empty array return?
What a strong answer covers
- The key observation: a group is valid exactly when every member contains one fixed digit
d, so there is no need to search subsets. - For each digit 0 to 9, count the numbers containing it, using a set of the number's digits so
55counts once. The answer is the largest count. - O(n) time and O(1) space (ten counters).
- Edge cases: empty input, one element, every number with a repeated digit.
Follow-ups
- Numbers can have any number of digits. What changes?
- Return the group itself, not only its size.
- The rule becomes "linked when two numbers share a digit, and links chain": find the largest connected group (union-find over digits).
Where it's asked
- Google · HackerRank assessment · Last asked Dec 202590 min · Two problems on HackerRank · Some early-career assessments enable the built-in assistant; others forbid AI. The invitation says which.
What it tests
graphs · union-find
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
