KatInterview

Largest Cluster of Two-Digit Numbers Linked by Common Digits

Trees & GraphsAuto-graded · 22 test cases
EasyAuto-gradedTrees & Graphsunion-findconnected-componentsbitmask

Last updated

Find the largest connected cluster in a list of two-digit numbers where links come from shared digits, testing graph connectivity modeling with union-find or digit-hub nodes.

You are given a list nums of two-digit integers. Think of each position in the list as its own item.

Two items are linked if their numbers have at least one decimal digit in common. It does not matter whether the shared digit is in the tens place or the ones place in either number. For instance, 37 and 73 are linked, and so are 41 and 14. Numbers like 20 and 35 are not linked.

A cluster is a collection of items that are tied together through chains of links. If item a is linked to b, and b is linked to c, then a, b, and c are all in the same cluster, even if a and c have no digit in common.

Return the number of items in the largest cluster.

Notes

  • Items are identified by position, not by value. If the same value appears several times, each occurrence is a separate item and each one counts toward the size of its cluster.
  • A number whose two digits are identical (such as 77) simply contains that single digit.
  • The digit 0 is an ordinary digit (for example, 30 and 50 are linked).
  • A list with a single item has a largest cluster of size 1.

Constraints

  • 1 <= len(nums) <= 100
  • Each value in nums is an integer from 10 to 99.
  • The function returns a single integer.

Examples

Example 1

Input: largest_digit_cluster([55, 58, 25, 45, 66])

Output: 4

Example 2

Input: largest_digit_cluster([12, 23, 34, 45])

Output: 4

Example 3

Input: largest_digit_cluster([11, 11, 22, 33, 44])

Output: 2

Hints

Hint 1

Model each list position as a node and add an edge when two numbers have a digit in common. The answer is the size of the biggest connected component.

Hint 2

Convert each number into the set of digits it contains (or a 10-bit mask). Two numbers are linked exactly when these sets intersect (or the masks AND to a nonzero value).

Hint 3

Merge linked indices with union-find (or run DFS/BFS) while tracking component sizes. Do not deduplicate values, since equal numbers are separate items.