Skip to main content
mediumGrind 75

Accounts Merge

Given accounts where accounts[i] = [name, email1, email2, ...] Merge accounts belonging to same person (same email) Return merged accounts: [name, sorted_emails...] Order of accounts doesn't matter

Given an array of accounts, each represented by a name followed by one or more email strings, merge accounts connected by shared email addresses, including indirect connections. Matching names alone do not mean accounts belong together. Return an array of merged accounts, each containing the name followed by its distinct email addresses in ascending lexicographic order. The merged accounts may appear in any order.

Examples

You write

accounts_merge(accounts: List[List[str]])

  1. Example 1

    Input
    accounts = [["John","johnsmith@mail.com","john_newyork@mail.com"],["John","johnsmith@mail.com","john00@mail.com"],["Mary","mary@mail.com"],["John","johnnybravo@mail.com"]]
    returns
    [["John","john00@mail.com","john_newyork@mail.com","johnsmith@mail.com"],["Mary","mary@mail.com"],["John","johnnybravo@mail.com"]]
  2. Example 2

    Input
    accounts = [["Gabe","Gabe0@m.co","Gabe3@m.co","Gabe1@m.co"],["Kevin","Kevin3@m.co","Kevin5@m.co","Kevin0@m.co"],["Ethan","Ethan5@m.co","Ethan4@m.co","Ethan0@m.co"]]
    returns
    [["Ethan","Ethan0@m.co","Ethan4@m.co","Ethan5@m.co"],["Gabe","Gabe0@m.co","Gabe1@m.co","Gabe3@m.co"],["Kevin","Kevin0@m.co","Kevin3@m.co","Kevin5@m.co"]]

The pattern behind it

Arrays & Hashing

I reach for it when

  • You catch yourself scanning the array again for every element.
  • The problem asks about duplicates, pairs, counts or groups.

Say this before you type. Say what the key is and what the value remembers, before you write the loop.

All 127 Arrays & Hashing problems

Strings

I reach for it when

  • The input is text and the answer is a count, a substring or a new string.
  • You need to compare, reverse or rebuild characters.

Say this before you type. Say which array pattern the string is hiding before you touch a character.

All 51 Strings problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1Merge Accounts Using Union-Findmedium

Prep guides that list this problem

Our company tags come from public interview write-ups. None of these is a confirmed question.