721. Accounts Merge
mediumAsked at MetaGiven accounts with emails, merge accounts that share any email (transitively). Meta asks this to test whether you reach for union-find or BFS/DFS on an implicit graph and articulate the connected-components framing.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Meta loops.
- Glassdoor (2026-Q1)— Meta E4 onsite reports cite this as the union-find / graph round.
- Blind (2025-11)— Recurring Meta interview problem.
Problem
Given a list 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. 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.
Constraints
1 <= accounts.length <= 10002 <= accounts[i].length <= 101 <= accounts[i][j].length <= 30accounts[i][0] consists of English letters.accounts[i][j] (for j > 0) is a valid email.
Examples
Example 1
accounts = [["John","[email protected]","[email protected]"],["John","[email protected]","[email protected]"],["Mary","[email protected]"],["John","[email protected]"]][["John","[email protected]","[email protected]","[email protected]"],["Mary","[email protected]"],["John","[email protected]"]]Approaches
1. Union-find (optimal)
Treat each account as a node. Union accounts that share at least one email. After unioning, group emails by root.
- Time
- O(N * K * α(N))
- Space
- O(N * K)
function accountsMerge(accounts) {
const parent = Array.from({ length: accounts.length }, (_, i) => i);
function find(x) {
while (parent[x] !== x) { parent[x] = parent[parent[x]]; x = parent[x]; }
return x;
}
function union(a, b) {
const ra = find(a), rb = find(b);
if (ra !== rb) parent[ra] = rb;
}
const emailToAcc = new Map();
for (let i = 0; i < accounts.length; i++) {
for (let j = 1; j < accounts[i].length; j++) {
const email = accounts[i][j];
if (emailToAcc.has(email)) union(i, emailToAcc.get(email));
else emailToAcc.set(email, i);
}
}
const groups = new Map();
for (const [email, idx] of emailToAcc) {
const root = find(idx);
if (!groups.has(root)) groups.set(root, []);
groups.get(root).push(email);
}
const result = [];
for (const [root, emails] of groups) {
emails.sort();
result.push([accounts[root][0], ...emails]);
}
return result;
}Tradeoff: Union-find is the natural fit because the operation is 'merge sets sharing an element.' Path compression + small lookup overhead = near-linear total complexity.
2. DFS on email graph
Build a graph where each email connects to all other emails in the same account. DFS each component.
- Time
- O(N * K * log(N * K))
- Space
- O(N * K)
function accountsMergeDFS(accounts) {
const graph = new Map();
const emailToName = new Map();
for (const acc of accounts) {
const name = acc[0];
for (let i = 1; i < acc.length; i++) {
const email = acc[i];
emailToName.set(email, name);
if (!graph.has(email)) graph.set(email, new Set());
if (i > 1) {
graph.get(email).add(acc[1]);
graph.get(acc[1]).add(email);
}
}
}
const visited = new Set();
const result = [];
for (const email of graph.keys()) {
if (visited.has(email)) continue;
const stack = [email];
const component = [];
while (stack.length) {
const e = stack.pop();
if (visited.has(e)) continue;
visited.add(e);
component.push(e);
for (const next of graph.get(e)) if (!visited.has(next)) stack.push(next);
}
component.sort();
result.push([emailToName.get(email), ...component]);
}
return result;
}Tradeoff: Same complexity asymptotically. Easier to whiteboard for candidates who haven't memorized union-find. Slightly more memory for the explicit graph.
Meta-specific tips
Meta interviewers want you to recognize this as a connected-components problem. State out loud: 'Each account is a node. Two accounts are connected iff they share an email. I need the connected components of this graph.' Then pick union-find (preferred) or DFS. Union-find is shorter and more impressive when you write it cleanly.
Common mistakes
- Forgetting to sort the emails in each merged group (the problem requires sorted output).
- Using the name to disambiguate accounts — names CAN repeat across people (two Johns).
- Building a graph where each email connects to every other email (O(K^2) per account); connecting via a hub email is enough.
Follow-up questions
An interviewer at Meta may pivot to one of these next:
- What if accounts could also have phone numbers (multiple keys)?
- What if the input is streaming (incremental merging)?
- Number of Connected Components in an Undirected Graph (LC 323) — same primitive.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Union-find or DFS — which does Meta prefer?
Both score equally. Union-find is shorter (~20 lines) and more impressive if bug-free. DFS is easier to whiteboard if you haven't practiced UF.
Why disambiguate accounts (not just emails)?
Different people can have the same name. The accounts are the entities; emails are the bridge between them. We union accounts, not names.
Practice these live with InterviewChamp.AI
Drill Accounts Merge and other Meta interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →