Accounts Merge
Updated
Problem statement
Each account is a list of strings. The first string is a person's name and every remaining string is an email address owned by that person.
Two accounts belong to the same person when they share at least one email address. This relationship is transitive: if account A shares an email with account B and account B shares an email with account C, all three accounts must be merged.
Return the merged accounts. Each returned account must contain:
- the person's name; then
- all unique email addresses in lexicographically ascending order.
To make the output the same every time, sort the returned accounts by name and then by their first email address. The input guarantees that every occurrence of one email address has the same owner name.
Function
accountsMerge(accounts: List<List<String>>) → List<List<String>>Examples
Example 1
accounts = [["John","johnsmith@mail.com","john_newyork@mail.com"],["John","johnsmith@mail.com","john00@mail.com"],["Mary","mary@mail.com"],["John","johnnybravo@mail.com"]]return = [["John","john00@mail.com","john_newyork@mail.com","johnsmith@mail.com"],["John","johnnybravo@mail.com"],["Mary","mary@mail.com"]]The first two John accounts share johnsmith@mail.com and merge. The other John account shares no email and remains separate.
Example 2
accounts = [["Alex","a@mail.com"],["Alex","b@mail.com"],["Alex","a@mail.com","c@mail.com"]]return = [["Alex","a@mail.com","c@mail.com"],["Alex","b@mail.com"]]The first and third accounts merge through a@mail.com. Equal names alone do not merge accounts.
Example 3
accounts = [["Solo","only@mail.com"]]return = [["Solo","only@mail.com"]]A single account is already a complete merged component.
Constraints
1 <= accounts.length <= 1000.2 <= accounts[i].length <= 10.- Names and emails are non-empty strings.
- The total number of email entries is at most
10^4. - Every email address belongs to exactly one owner name.
More Oracle Onsite Interview questions
- Course ScheduleONSITE INTERVIEW · Seen Nov 2026
- Shortest Subarray with Sum at Least KONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Minimum Meeting RoomsONSITE INTERVIEW · Seen Nov 2026
- Shortest Window with Required CountsONSITE INTERVIEW · Seen Nov 2026
- Maximum Subarray Sum with One DeletionONSITE INTERVIEW · Seen Nov 2026
- Shortest Deletion for Distinct CharactersONSITE INTERVIEW · Seen Nov 2026
- Count Dropped Rate-Limited RequestsONSITE INTERVIEW · Seen Nov 2026
- Design Authentication ManagerONSITE INTERVIEW · Seen Oct 2026
- Maximum Bouquet ValueONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Find All People With SecretIDFC Bank · ONSITE INTERVIEWIDFC Bank interview questions
- Group Linked Merchant RecordsStripe · PHONE SCREENStripe interview questions
- Top K Frequent ElementsAmazon · OA · PHONE SCREEN · ONSITE INTERVIEW
- Mutual-Friend Referral RecommendationsFireworks AI · PHONE SCREEN · ONSITE INTERVIEW
- Group AnagramsAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Filter, Sort, and Deduplicate Scheduled TasksRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Count Zero Request ServersSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Group AnagramsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Radar Barrier CrossingSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions