← PayPal bank

PayPal · Question 75 · Graphs

Accounts Merge

GraphsJavacode⏱ O(A*α(A))💾 O(A)
Product-based

Problem

Merge accounts where multiple emails belong to the same user.

Approach

Use Union - Find (Disjoint Set Union) to merge emails.

Time O(A*α(A))Space O(A)

How it works

  1. 1Union emails sharing an account.
  2. 2Group by root; TreeSet sorts each group.
  3. 3Prepend the owner name; driver sorts groups.

Sample input / output

Input:
accounts = 4 (John x3, Mary x1)
Output:
[[John, john00@mail.com, john_newyork@mail.com, johnsmith@mail.com], [John, johnnybravo@mail.com], [Mary, mary@mail.com]]

Constraints

1 <= accounts.length <= 1000; 2 <= account.length <= 10; lowercase emails.

Solution — read it, then walk it

Time O(A*α(A))Space O(A)Loops 7Branches 1Returns 2source · Java
Scroll sideways for wide charts — the view follows the active step.
↻ repeat↻ repeat↻ repeat↻ repeat↻ repeat↻ repeat↻ repeatyesnoSTARTimport java.util.*DEFINE class MainDEFINE staticList<List<String>>accountsMerge(List<List<String>…Map<String, String>emailToName = newHashMap<>()Map<String, String> parent= new HashMap<>()for (List<String>account : accounts)String name =account.get(0)for (int i = 1; i <account.size(); i++)parent.put(account.get(i),account.get(i))emailToName.put(account.get(i),name)for (List<String>account : accounts)String firstEmail =account.get(1)for (int i = 2; i <account.size(); i++)union(parent, firstEmail,account.get(i))Map<String,TreeSet<String>> unions =new HashMap<>()for (String email :parent.keySet())String root = find(parent,email)unions.computeIfAbsent(root,k -> newTreeSet<>()).add(email)List<List<String>> result =new ArrayList<>()for (String root :unions.keySet())List<String> emails = newArrayList<>(unions.get(root))emails.add(0,emailToName.get(root))result.add(emails)RETURN resultDEFINE static Stringfind(Map<String, String>parent, String s)while(!parent.get(s).equals(s))parent.put(s,parent.get(parent.get(s)))s = parent.get(s)RETURN sDEFINE static voidunion(Map<String, String>parent, String a, String b)String rootA = find(parent,a)String rootB = find(parent,b)if(!rootA.equals(rootB))parent.put(rootA, rootB)DEFINE static voidmain(String[] args)List<List<String>> accounts= new ArrayList<>()accounts.add(Arrays.asList("John","johnsmith@mail.com","john00@mail.com"))accounts.add(Arrays.asList("John","johnnybravo@mail.com"))accounts.add(Arrays.asList("John","johnsmith@mail.com","john_newyork@mail.com"))accounts.add(Arrays.asList("Mary","mary@mail.com"))List<List<String>> merged =accountsMerge(accounts)merged.sort((a, b) ->a.get(1).compareTo(b.get(1)))System.out.println(merged)END
  • start / end
  • statement
  • condition
  • loop
  • returns
  • definition
1/44

StartSTART

Interactive control-flow diagram and step-by-step code walkthrough.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits