Merge Duplicate User Accounts That Share Any Email

Last updated: November 29, 2026

Quick Overview

Each account is a name followed by email addresses. Two accounts belong to the same person if they share at least one email, and this relation is transitive. Return the merged accounts with each person's emails sorted.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Each account is a name followed by email addresses. Two accounts belong to the same person if they share at least one email, and this relation is transitive. Return the merged accounts with each person's emails sorted.

Identity resolution across signups is a real concern for Meta's account and integrity teams, and this question tests connected components on an implicit graph. Interviewers look for union-find or BFS over an email graph, correct handling of transitive links, and a clean complexity analysis.

What the Interviewer Expects
  • Models emails as graph nodes linked within an account
  • Uses union-find with path compression, or BFS/DFS
  • Captures transitive merges, not just pairwise overlap
  • Keeps the name attached to each component
  • Sorts emails per component and analyzes the cost
Key Topics to Cover
Union-find
Graphs
Connected components
Hash map
Sorting
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Accounts arrive incrementally; how do you merge online?
  • Two different people share a family email; how would you avoid over-merging?
  • Scale this to billions of accounts across machines.
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: list of accounts, each [name, email1, ...]. Names are not unique identifiers; only shared emails merge accounts. Output: list of [name, sorted emails...] in any order. Duplicated emails wit...

High-Level Design

Assign each email to a disjoint-set node. For each account, union every email with the first email of that account. Afterward, emails with the same root form one person. Group by root, attach the name...


Submit Your Answer
Markdown supported