Combine Accounts Connected by Shared Addresses

Quick Overview

Combine Accounts Connected by Shared Addresses. Each account contains a name followed by email addresses.

Combine Accounts Connected by Shared Addresses

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Each account contains a name followed by email addresses. Accounts sharing an address belong to the same person, transitively. Matching names alone do not establish identity. A person's accounts have the same name. Return one row per merged person, beginning with the name and followed by unique addresses in sorted order. Merged rows may appear in any order. ```cpp class Solution { public: vector<vector<string>> accountsMerge(vector<vector<string>>& accounts); }; ``` ### Examples **Example 1:** ```text accounts = [["Ada","b@example.com","a@example.com"],["Ada","b@example.com","c@example.com"]] One valid output: [["Ada","a@example.com","b@example.com","c@example.com"]] Explanation: The shared b address connects both rows. ``` **Example 2:** ```text accounts = [["Bo","x@example.com"],["Bo","y@example.com"]] One valid output: [["Bo","x@example.com"],["Bo","y@example.com"]] Explanation: Equal names do not merge distinct address sets. ``` ### Constraints - There are 1 through 1000 account rows, each containing 2 through 10 strings. - Each string has length 1 through 30. - Names use English letters; later strings are valid email addresses. ### Contract considerations Preserve the input row schema, unique addresses and within-row sorting. ```hint Read the boundary condition Which graph connectivity relation is established by one common address? ``` ### What a Strong Answer Covers Preserve the input row schema, unique addresses and within-row sorting. ### Follow-up Questions Optional practice extension: How would you accept additional accounts incrementally while retaining exact email-based connectivity?

Overview: Combine Accounts Connected by Shared Addresses. Each account contains a name followed by email addresses.

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
1
0

Problem

Each account contains a name followed by email addresses. Accounts sharing an address belong to the same person, transitively. Matching names alone do not establish identity. A person's accounts have the same name. Return one row per merged person, beginning with the name and followed by unique addresses in sorted order. Merged rows may appear in any order.

class Solution {
public:
    vector<vector<string>> accountsMerge(vector<vector<string>>& accounts);
};

Examples

Example 1:

accounts = [["Ada","b@example.com","a@example.com"],["Ada","b@example.com","c@example.com"]]
One valid output: [["Ada","a@example.com","b@example.com","c@example.com"]]
Explanation: The shared b address connects both rows.

Example 2:

accounts = [["Bo","x@example.com"],["Bo","y@example.com"]]
One valid output: [["Bo","x@example.com"],["Bo","y@example.com"]]
Explanation: Equal names do not merge distinct address sets.

Constraints

  • There are 1 through 1000 account rows, each containing 2 through 10 strings.
  • Each string has length 1 through 30.
  • Names use English letters; later strings are valid email addresses.

Contract considerations

Preserve the input row schema, unique addresses and within-row sorting.

What a Strong Answer Covers Guidance

Preserve the input row schema, unique addresses and within-row sorting.

Follow-up Questions Guidance

Optional practice extension: How would you accept additional accounts incrementally while retaining exact email-based connectivity?