Accounts merge
Python data engineering interview problem. Difficulty: advanced. Pattern: Graphs. About 18 minutes. Part of the Pro drill bank.
Merge customer accounts that share at least one common email into unified profiles with sorted emails. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.
Implement accounts_merge(accounts: list[list[str]]) -> list[list[str]] to reconcile duplicate user accounts. You are given a list of accounts where each element accounts[i] is a list of strings: The first element accounts[i][0] is a person's name. The remaining elements accounts[i][1:] are emails belonging to that account. If two accounts share any common email, they belong to the same person. Two accounts may share the same name without being the same person if they share no emails. Return the merged accounts: Each merged entry starts with the name, followed by its emails in sorted order. To make verification deterministic, sort the list of merged accounts primarily by the name, and secondarily by the account's first email.
Requirements
- Emails in each merged account must be sorted.
- Accounts sharing an email must be combined under that name.
Constraints
- 1 <= len(accounts) <= 1,000
- 2 <= len(accounts[i]) <= 10
- Email lengths between 1 and 30 characters.
- Names consist of English letters only.
Examples
Input: accounts_merge([['John', '[email protected]', '[email protected]'], ['John', '[email protected]', '[email protected]'], ['Mary', '[email protected]'], ['John', '[email protected]']]) Output: [['John', '[email protected]', '[email protected]', '[email protected]'], ['John', '[email protected]'], ['Mary', '[email protected]']] The first two John accounts share [email protected] and merge into one, while the third John has distinct emails and remains separate.
Topics: lakebench, python, connected-components, union-find.
More Python interview questions · All interview problems · Learn data engineering
Accounts merge
Interview-style drill: Merge customer accounts that share at least one common email into unified profiles with sorted emails.
Implement `accounts_merge(accounts: list[list[str]]) -> list[list[str]]` to reconcile duplicate user accounts. You are given a list of accounts where each element `accounts[i]` is a list of strings: - The first element `accounts[i][0]` is a person's name. - The remaining elements `accounts[i][1:]` are emails belonging to that account. If two accounts share any common email, they belong to the same person. Two accounts may share the same name without being the same person if they share no emails. Return the merged accounts: - Each merged entry starts with the name, followed by its emails in sorted order. - To make verification deterministic, sort the list of merged accounts primarily by the name, and secondarily by the account's first email.