DSA Tracker

Medium

Accounts Merge

A medium Graph problem included in Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.

Topic
Graph
Sheets
1
Core for
11 roles
Platform
LeetCode

The problem

Given a list of accounts where each account contains a name and a list of emails, merge accounts that share at least one common email. Return the merged accounts with emails sorted in ascending order.

Example 1

Input
accounts = [["John","john@example.com","john_new@example.com"],["John","john@example.com","john00@example.com"],["Mary","mary@example.com"]]
Output
[["John","john00@example.com","john@example.com","john_new@example.com"],["Mary","mary@example.com"]]
Why
The two John accounts share john@example.com and are merged.

Example 2

Input
accounts = [["David","David0@m.co","David1@m.co"],["David","David3@m.co","David4@m.co"],["David","David5@m.co","David6@m.co"]]
Output
[["David","David0@m.co","David1@m.co","David3@m.co","David4@m.co","David5@m.co","David6@m.co"]]
Why
All three David accounts share emails and are merged into one.

Constraints

  • 1 <= accounts.length <= 1000
  • 2 <= accounts[i].length <= 10

How to think about it

Updated 2026-09-09

Names are not unique identifiers; only shared emails establish equivalence. Two accounts belong to the same person if they share any email, either directly or through an unbroken chain of shared emails. Connect the first email of each account to all other emails in that account to form connected components, then sort each component's emails and attach the person's name.

Approaches, worst first

  1. Graph DFS component collection

    time O(N * K log(N * K)) · space O(N * K)

    Build an undirected graph where each email is a node and edges connect emails appearing in the same account. Also map each email to the account's name. Run DFS on unvisited emails to gather components, sort them, and prepend the name.

  2. Disjoint Set Union by email indexWrite this one

    time O(N * K * α(N * K) + N * K log(N * K)) · space O(N * K)

    Map every email to an integer ID and to its account name. Within each account, union the ID of its first email with the IDs of all subsequent emails. Group emails by their DSU root, sort each group, and prepend the account name.

Where people lose marks · 3
  • Two people can share the same name (e.g. two different people named 'John') without sharing emails; grouping by name instead of by email graph merges distinct individuals.
  • The problem requires emails within each merged account to be sorted lexicographically; failing to sort breaks test assertions.
  • An account might list duplicate emails within its own list; deduplicate emails or handle parallel edges safely.

The theory behind it

Graph — the ground this problem stands on. All Graph problems

What Graph is

A graph is a network of individual points, called vertices or nodes, connected by lines called edges. Think of a subway transit map, an electrical circuit, or a web of social friends. Unlike a tree, a graph has no designated top node and no parent-child hierarchy. Connections can run one-way or both ways, and paths can loop back on themselves to form closed cycles.

When to reach for it

Reach for graph algorithms when inputs describe relationships, networks, flights between cities, course prerequisites, or clone networks. Signals include finding the shortest route across unweighted connections, ordering tasks that depend on earlier tasks, counting isolated clusters, or checking whether a path contains an infinite loop. Whenever problems present pairs of related entities and ask for reachability, distances, or dependencies, graph representations apply.

How the pattern works

First convert edge lists into an adjacency list, mapping each node to an array of its neighbors. Choose your exploration strategy based on the goal: use a queue and breadth-first search to find the shortest path in unweighted networks, or use recursion and depth-first search to explore full paths and detect cycles. Because graphs can have loops, always track visited nodes in a set or boolean array. Add nodes to the visited set at the moment they enter the queue so they are never visited twice.

What each operation costs

OperationTime
visit all nodes and edges via searchO(v + e)
topological sort using in-degree countsO(v + e)
shortest path using dijkstra with a min-heapO((v + e) log v)
What usually goes wrong with Graph
  • Adding a node to the visited set when popping from the queue instead of when pushing, which lets neighboring nodes enqueue duplicate entries and wastes memory.
  • Failing to check for cycles in directed graphs when finding prerequisite orders, causing topological sort routines to hang or return incomplete lists.
  • Assuming an input graph is fully connected and scanning from only a single starting node, missing disconnected islands and isolated components.

Which roles need this problem

Graph is a core topic for these 11 roles — if you're targeting one of them, this problem is early in your path, not optional.

Secondary for 6 more roles, including Performance Engineer, Search Engineer, Information Retrieval Engineer.

Track this in your role's order

Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.

Start free

More Graph problems

Problem set and role mapping as of .