Skip to main content

721. Accounts Merge

medium

Given a list of accounts where each has a name and a list of emails, merge accounts that share any email. Real-world identity-resolution problem mapped onto Union-Find — emails as nodes, account-membership as the link signal.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Problem

Given a list of accounts where each element accounts[i] is a list of strings, where the first element accounts[i][0] is a name, and the rest of the elements are emails representing emails of the account. Now, we would like to merge these accounts. Two accounts definitely belong to the same person if there is some common email to both accounts. Note that even if two accounts have the same name, they may belong to different people as people could have the same name. A person can have any number of accounts initially, but all of their accounts definitely have the same name. After merging the accounts, return the accounts in the following format: the first element of each account is the name, and the rest of the elements are emails in sorted order. The accounts themselves can be returned in any order.

Constraints

  • 1 <= accounts.length <= 1000
  • 2 <= accounts[i].length <= 10
  • 1 <= accounts[i][j].length <= 30
  • accounts[i][0] consists of English letters.
  • accounts[i][j] (for j > 0) is a valid email.

Examples

Example 1

Explanation: The first and second John's are the same person as they have the common email '[email protected]'. The third John and Mary are different people as none of their email addresses are used by other accounts.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

Hints

Progressive — try the first before opening the next.

Hint 1

Treat each email as a node. Within an account, every email is in the same component — union the first email with each subsequent one.

Hint 2

After all unions, group emails by their DSU root. Each group is one merged account.

Hint 3

Recover the name by mapping every email back to the account it first appeared in.

Solution approach

Reveal approach

Union-Find on emails. Maintain three maps: email -> id (assign integer ids on first sight), email -> name (account owner). For each account, take the first email as the anchor and union every other email in the account with it. After processing all accounts, group emails by their DSU root: walk emails, find their root, and append to roots[root] -> [emails]. Sort each group's emails alphabetically and prepend the name. Return the list. The grouping step naturally collapses the transitive-closure semantics — two accounts that share even one email end up in the same group. O(N * K * alpha(N*K)) where N is account count and K is average emails per account.

Complexity

Time
O(N * K * alpha(N*K))
Space
O(N * K)

Related patterns

  • union-find
  • graph
  • dfs
  • hash-map

Related problems

Asked at

Companies reported asking this problem (sourced from public Glassdoor, Blind, and Levels.fyi interview posts).

  • Amazon
  • Google
  • Meta
  • LinkedIn

Practice these live with InterviewChamp.AI

Drill Accounts Merge and Graphs problems under real interview conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →