PayPal · Question 75 · Graphs
Accounts Merge
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
- 1Union emails sharing an account.
- 2Group by root; TreeSet sorts each group.
- 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.
- 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