KatInterview

Merge Accounts Into Groups via Weighted Field Matching

Trees & GraphsAuto-graded · 23 test cases
MediumAuto-gradedTrees & Graphsunion-findconnected-componentsgraph

Last updated

Group records into clusters where records are linked when their name/email match score reaches a cutoff, using union-find and bucketing on exact field values.

Merge Accounts Into Groups via Weighted Field Matching

You manage a collection of account records. Each record is a dictionary with three keys:

  • id: a unique integer
  • name: a string
  • email: a string

For any two different records, a match score is computed:

  • Add 0.6 if their email strings are exactly equal.
  • Add 0.4 if their name strings are exactly equal.

So the score is 0.0, 0.4, 0.6, or 1.0.

Two records are directly linked when their score is greater than or equal to the given cutoff.

A group is a maximal set of records connected through direct links, possibly through intermediaries. If A is linked to B and B is linked to C, then A, B, and C belong to the same group, even if A and C are not directly linked.

Every record belongs to exactly one group, and a record with no links forms a group by itself.

Task

Implement group_linked_accounts(accounts, cutoff), which returns the groups as a list of lists of id values.

Output format

  • Inside each group, list the ids in ascending order.
  • Order the groups by their smallest id, ascending.
  • Return [] if accounts is empty.

Rules and edge cases

  • Matching is exact and case-sensitive. No normalization is applied, and an empty string matches another empty string.
  • If cutoff <= 0.0, even a score of 0.0 qualifies, so all records form one group.
  • If cutoff > 1.0, no pair can be linked, so every record is its own group.
  • Compare against the exact scores 0.4, 0.6, and 1.0. For example, a cutoff of 0.4 links records that share a name, and a cutoff of 0.5 links only records that share an email.

Constraints

  • 0 <= len(accounts) <= 10,000
  • Each id is a unique integer.
  • name and email are strings.
  • cutoff is a float, normally in [0.0, 1.0], but values outside that range must be handled as described above.
  • An approach that compares every pair of records is too slow to rely on. Aim for near-linear time.

Examples

Example 1

Input: group_linked_accounts([{'id': 1, 'name': 'Alice', 'email': '[email protected]'}, {'id': 2, 'name': 'Alice', 'email': '[email protected]'}, {'id': 3, 'name': 'Bob', 'email': '[email protected]'}], 0.4)

Output: [[1, 2, 3]]

Example 2

Input: group_linked_accounts([{'id': 1, 'name': 'Alice', 'email': '[email protected]'}, {'id': 2, 'name': 'Bob', 'email': '[email protected]'}, {'id': 3, 'name': 'Carol', 'email': '[email protected]'}], 0.6)

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

Example 3

Input: group_linked_accounts([{'id': 10, 'name': 'A', 'email': 'x'}, {'id': 2, 'name': 'A', 'email': 'y'}, {'id': 7, 'name': 'B', 'email': 'x'}, {'id': 4, 'name': 'C', 'email': 'z'}], 0.7)

Output: [[2], [4], [7], [10]]

Hints

Hint 1

Think of each record as a graph node. The answer is the set of connected components, and union-find (disjoint set union) computes them directly.

Hint 2

The score can only be 0.0, 0.4, 0.6, or 1.0, so the cutoff determines which fields must match. Work out which kind of match is enough for each cutoff range.

Hint 3

Instead of checking pairs, bucket records by the key that must match (name, email, or the pair) and union everything in each bucket with its first member. When a name match alone suffices, an email match also does, so use both buckets.