EvalFrame Technical Mock Interviews & Coding Practice

Accounts Merge

Medium · Graph / Disjoint Set · Algorithm problem · all problems

Given a list of `accounts` where each element `accounts[i]` is a list of strings, where the first element `accounts[i][0]` is a name, and the rest of the elements are emails representing emails of the account. Merge accounts belonging to the same person. Two accounts belong to the same person if there is some common email to both accounts. Return the accounts in sorted email order. **Examples:** ```typescript accountsMerge([ ["John","[email protected]","[email protected]"], ["John","[email protected]","[email protected]"], ["Mary","[email protected]"], ["John","[email protected]"] ]) // [ // ["John","[email protected]","[email protected]","[email protected]"], // ["Mary","[email protected]"], // ["John","[email protected]"] // ] ```

Solve It in the BrowserPractice Explaining It Out Loud

Optimal Approach

  • Time complexity: O(N log N)
  • Space complexity: O(N)

Union-find links emails in nearly linear time; sorting grouped emails takes O(N log N).

Follow-Up Questions Interviewers Ask About Accounts Merge

  • Could this also be modeled as an undirected graph with connected components?
    Yes, treat emails as graph vertices connected by edges; DFS or BFS will traverse and collect each connected component.

Practice the full problem with execution, tests, and AI code review in the interactive lab — then rehearse explaining your solution in a live voice mock interview.