KatInterview

Group Accounts by Shared Identity Fields

Trees & GraphsAuto-graded · 15 test cases
MediumAuto-gradedTrees & Graphsunion-findconnected-componentshashing

Last updated

Tests the ability to model transitive relationships between records using Union-Find (DSU) to identify connected components linked by shared attribute values.

Problem Statement

You are given a list of account records. Each record is a dictionary with four keys:

  • "id" — a unique integer identifier for the account
  • "tax_id" — a string, or None
  • "email" — a string, or None
  • "phone" — a string, or None

Two accounts are considered related if they share the same non-None value in at least one field (either both have the same tax_id, both have the same email, or both have the same phone).

Relatedness is transitive: if account A is related to B and B is related to C, then A, B, and C all belong to the same cluster, even if A and C share no fields directly.

Write a function that takes this list of account records and returns a list of clusters. Each cluster is a sorted list of id values belonging to the same group. The outer list must be sorted in ascending order by each cluster's first element.

Class / Record Format

Each record is a plain Python dict:

{
    "id":     int,
    "tax_id": str | None,
    "email":  str | None,
    "phone":  str | None,
}

Rules and Edge Cases

  • None values are ignored when determining relatedness. Two accounts that both have None for a field are not linked through that field.
  • A "tax_id" of "123" and a "phone" of "123" do not link two accounts. A match only counts when the same field carries the same value.
  • An account whose tax_id, email, and phone are all None, or whose values are all unique, forms a cluster of exactly one.
  • Chains of indirect links must be followed to their full extent: if A–B share a tax_id, and B–C share a phone, then A, B, and C are one cluster.

Constraints

  • Number of accounts n ≤ 10,000
  • Each "id" is a unique positive integer
  • Field values, when not None, are non-empty strings
  • An empty input list returns an empty list
  • Output: a list of lists; each inner list is sorted; the outer list is sorted by its first element

Examples

Example 1

Input: group_accounts_by_shared_fields([{'id': 1, 'tax_id': 'T1', 'email': '[email protected]', 'phone': None}, {'id': 2, 'tax_id': 'T1', 'email': None, 'phone': '123'}, {'id': 3, 'tax_id': None, 'email': None, 'phone': '456'}])

Output: [[1, 2], [3]]

Example 2

Input: group_accounts_by_shared_fields([{'id': 1, 'tax_id': None, 'email': '[email protected]', 'phone': None}, {'id': 2, 'tax_id': 'T2', 'email': '[email protected]', 'phone': None}, {'id': 3, 'tax_id': 'T3', 'email': None, 'phone': None}])

Output: [[1, 2], [3]]

Hints

Hint 1

Think about what data structure naturally captures 'same group as' relationships and supports merging groups efficiently.

Hint 2

Instead of comparing every pair of accounts (O(n²)), build a map from (field_name, value) to the first account index that had that value — then you only need one pass to find and union matches.

Hint 3

Make sure your lookup key includes the field name, not just the value. A tax_id of '555' and a phone of '555' are different keys and should not cause a link.