My first instinct was to just do it for emails like the standard version, and I literally forgot to handle phone numbers as a separate identifier type for a solid two minutes.
Model the problem as a graph where each account is a node and shared contacts create edges, then find connected components using Union-Find (Disjoint Set Union) for near-linear time. For each component, merge the names and union all contacts, ensuring no duplicates. This approach efficiently handles large datasets and is easy to explain and implement.
Pro tip: Mention that Union-Find with path compression and union by rank gives almost O(N) time, and that you can map each contact to an account index to build the graph without pairwise comparisons. Also note that if names differ within a component, you can either keep the first name or flag it for review, showing awareness of real-world data quality issues.
Confirm that accounts sharing any email or phone should be merged, and that the merged account should contain the union of all contacts. Ask about handling conflicting names and whether the output order matters.
Iterate through each account and its contacts, storing for each unique email and phone the index of the account it belongs to. This mapping will be used to union accounts that share a contact.
For each contact, if it appears in multiple accounts, union those account indices using Union-Find. After processing all contacts, accounts in the same connected component are merged.
Group accounts by their root in the Union-Find structure. For each group, pick a representative name (e.g., the first account's name) and collect all unique emails and phone numbers from all accounts in the group.
Format the result as a list of merged accounts, each containing the name and the sorted list of unique contacts. Optionally, discuss time and space complexity.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.