KatInterview

Unify Contact Profiles Linked by Common Email Addresses

Trees & GraphsModel answer
MediumModel answerTrees & Graphsunion-findgraphconnected-components

Last updated

Combine contact records that are directly or transitively linked by a shared email, outputting each person's name with a deduplicated, sorted email list.

Consolidate Duplicate Profiles

A system stores profiles as lists of strings. The first string in each list is the profile's display name. Every following string is an email address tied to that profile.

Two profiles describe the same person whenever they have at least one email in common. This relation is transitive: if profile A shares an email with B, and B shares one with C, then A, B and C are all the same person, even if A and C have nothing in common directly.

Write a function that consolidates the profiles. Each consolidated profile must contain:

  • the display name, followed by
  • every distinct email from all the profiles that were merged into it, in ascending lexicographic order.
def merge_accounts(accounts: list[list[str]]) -> list[list[str]]:

Rules

  • Only a shared email links two profiles. Having the same name is never a reason to merge.
  • Two profiles with equal names but no email connection (direct or through a chain) must stay separate.
  • A single profile can list the same email more than once. The output must contain it only once.
  • The order of the returned profiles is not specified, but listing them by the position of each group's first profile is a sensible choice.

Constraints

  • 1 <= len(accounts) <= 1000
  • 2 <= len(accounts[i]) <= 10
  • 1 <= len(accounts[i][j]) <= 30

Examples

Example 1

Input: [['John', '[email protected]', '[email protected]'], ['John', '[email protected]', '[email protected]'], ['Mary', '[email protected]']]

Output: [['John', '[email protected]', '[email protected]', '[email protected]'], ['Mary', '[email protected]']]

Example 2

Input: [['Gabe', '[email protected]', '[email protected]'], ['Gabe', '[email protected]'], ['Gabe', '[email protected]', '[email protected]']]

Output (following the rules above): [['Gabe', '[email protected]', '[email protected]', '[email protected]'], ['Gabe', '[email protected]']]

The first and third profiles are joined through [email protected]. The middle profile shares no email with either, so it stays on its own even though the name matches.

What to cover in your answer

  1. How you model the problem and why it is correct.
  2. The algorithm, its time and space complexity, and working code.
  3. The edge cases you would check.
  4. How you would adapt the approach for a very large dataset, or if the output had to list a primary email first.

Hints

Hint 1

Think of the profiles (or the emails) as nodes in a graph. Which pairs should be joined by an edge, and what does a merged person correspond to in that graph?

Hint 2

Avoid comparing every pair of profiles. Instead, remember for each email which profile you saw it in first, and link any later profile that contains the same email to that one.

Hint 3

A disjoint set union over profile indices handles transitivity automatically. After all links are made, collect emails into a set per root, then output the name plus the sorted set.