Merge User Accounts That Share Any Email Address
Last updated: November 29, 2026
Quick Overview
Each account record has a display name followed by one or more email addresses. Two records belong to the same person if they share any email. Merge them and return each person's name with their sorted unique emails.
0
0
0 solved
Each account record has a display name followed by one or more email addresses. Two records belong to the same person if they share any email. Merge them and return each person's name with their sorted unique emails.
An onsite question echoing identity work behind Google Accounts, where a user may have signed up several times with overlapping addresses. The interviewer checks that you see a graph on emails, that connectivity is transitive, and that names alone are not identifiers.
What the Interviewer Expects
- Model emails as nodes and records as edges linking their emails
- Find connected components with DFS or union-find
- Not merge by name since different people share names
- Sort emails within each merged account
- Discuss complexity including the sort
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you do this incrementally as new sign-ups arrive?
- What if the input is too big for one machine?
- How do you handle a shared family email that wrongly merges two people?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input:
accounts, each[name, email1, email2, ...]. - Same person if any email overlaps, transitively.
- Output: list of
[name, *sorted_emails], any order of accounts....
High-Level Design
Build a graph whose nodes are emails. For each record, connect its first email to every other email in that record; a star is enough to keep the record's emails in one component. Then each connected c...