Back to Practice
#0297
Accounts Merge
MediumDSA28 min20 XP
Problem
Each account is [name, email1, email2, ...]. Merge accounts that share emails. Return sorted merged accounts as [name, sorted emails...], sorted by name then first email.
Why This Matters
Accounts Merge is a realistic union-find problem: entities connect through shared identifiers.
Function Signature
def accounts_merge(accounts):
Examples
Example 1
Inputaccounts = [["John","a@mail","b@mail"],["John","b@mail","c@mail"],["Mary","m@mail"]]
Output[["John", "a@mail", "b@mail", "c@mail"], ["Mary", "m@mail"]]
The two John accounts share b@mail, so all three John emails merge.
Constraints
- Return the exact requested value.
- Handle edge cases cleanly.
- Use the intended DSA pattern when brute force would scale poorly.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Shared email merge
InputJohn shares b@mail
Expectedmerged John and Mary
Shared email connects accounts into one component.
Separate same name
Inputsame name no shared email
Expectedtwo separate rows
Same name alone is not enough to merge.
Hidden Test Categories
chain of shared emailsmultiple namesemail order shuffled