Quick Overview

Group account records by transitive connections through shared attributes. Return consistently ordered account groups while handling duplicate records and keeping account identifiers separate from attribute values.

Group accounts connected by shared attributes

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

You are given records of the form `[account, attribute]`. Accounts with the same attribute are connected. Connectivity is transitive. If account `a` shares one attribute with `b`, and `b` shares another with `c`, all three belong to the same group. Return every connected group of accounts. An account may appear in several records with different attributes. ### Function Signature ```python def group_accounts(records: list[list[str]]) -> list[list[str]]: ``` ### Rules - Only accounts appearing in at least one record are included. - Repeated identical records do not change connectivity. - Account identifiers and attribute values occupy separate namespaces. The same string used as an account and an attribute creates no connection by itself. - Sort the account identifiers in each group in ascending lexicographic order. Sort the groups by their first identifier. Return `[]` for empty input. - The ordering and duplicate rules make the exercise self-contained. The report did not specify an output order. ### Constraints These are exercise bounds because the report does not give input limits. - `0 <= len(records) <= 100000`. - Each record contains exactly two strings. - Each string contains 1 to 30 lowercase ASCII letters or digits. ### Examples ```text Input: records = [["a", "x"], ["b", "x"], ["b", "y"], ["c", "y"], ["d", "z"]] Output: [["a", "b", "c"], ["d"]] ``` `a` connects to `b` through `x`. `b` connects to `c` through `y`. The account `d` shares no attribute with the other accounts. ```text Input: records = [["a", "x"], ["a", "x"], ["x", "y"]] Output: [["a"], ["x"]] ``` The repeated pair has no effect. The attribute `x` in the first record is different from the account identifier `x` in the last record.

Overview: Group account records by transitive connections through shared attributes. Return consistently ordered account groups while handling duplicate records and keeping account identifiers separate from attribute values.

Read the full Amazon Software Engineer interview experience this question came from

You are given records of the form `[account, attribute]`. Accounts with the same attribute are connected. Connectivity is transitive. If account `a` shares one attribute with `b`, and `b` shares another with `c`, all three belong to the same group. Return every connected group of accounts. An account may appear in several records with different attributes. ### Function Signature ```python def group_accounts(records: list[list[str]]) -> list[list[str]]: ``` ### Rules - Only accounts appearing in at least one record are included. - Repeated identical records do not change connectivity. - Account identifiers and attribute values occupy separate namespaces. The same string used as an account and an attribute creates no connection by itself. - Sort the account identifiers in each group in ascending lexicographic order. Sort the groups by their first identifier. Return `[]` for empty input. - The ordering and duplicate rules make the exercise self-contained. The report did not specify an output order. ### Constraints These are exercise bounds because the report does not give input limits. - `0 <= len(records) <= 100000`. - Each record contains exactly two strings. - Each string contains 1 to 30 lowercase ASCII letters or digits. ### Examples ```text Input: records = [["a", "x"], ["b", "x"], ["b", "y"], ["c", "y"], ["d", "z"]] Output: [["a", "b", "c"], ["d"]] ``` `a` connects to `b` through `x`. `b` connects to `c` through `y`. The account `d` shares no attribute with the other accounts. ```text Input: records = [["a", "x"], ["a", "x"], ["x", "y"]] Output: [["a"], ["x"]] ``` The repeated pair has no effect. The attribute `x` in the first record is different from the account identifier `x` in the last record.

Constraints

  • `0 <= len(records) <= 100000`.
  • Each record contains exactly two strings.
  • Each string contains 1 to 30 lowercase ASCII letters or digits.

Examples

Input: ([['a', 'x'], ['b', 'x'], ['b', 'y'], ['c', 'y'], ['d', 'z']],)

Expected Output: [['a', 'b', 'c'], ['d']]

Explanation: Accounts bridge attributes transitively.

Input: ([['a', 'x'], ['a', 'x'], ['x', 'y']],)

Expected Output: [['a'], ['x']]

Explanation: Account and attribute identifiers occupy separate namespaces.

Hints

  1. Sharing one attribute connects all accounts using it, even if only some edges are kept.
  2. Account IDs and attributes are independent kinds of keys.

Group accounts connected by shared attributes

hardAmazonSoftware Engineer

You are given records of the form [account, attribute]. Accounts with the same attribute are connected. Connectivity is transitive. If account a shares one attribute with b, and b shares another with c, all three belong to the same group.

Return every connected group of accounts. An account may appear in several records with different attributes.

Function Signature

def group_accounts(records: list[list[str]]) -> list[list[str]]:

Rules

  • Only accounts appearing in at least one record are included.
  • Repeated identical records do not change connectivity.
  • Account identifiers and attribute values occupy separate namespaces. The same string used as an account and an attribute creates no connection by itself.
  • Sort the account identifiers in each group in ascending lexicographic order. Sort the groups by their first identifier. Return [] for empty input.
  • The ordering and duplicate rules make the exercise self-contained. The report did not specify an output order.

Constraints

These are exercise bounds because the report does not give input limits.

  • 0 <= len(records) <= 100000.
  • Each record contains exactly two strings.
  • Each string contains 1 to 30 lowercase ASCII letters or digits.

Examples

Input: records = [["a", "x"], ["b", "x"], ["b", "y"], ["c", "y"], ["d", "z"]]
Output: [["a", "b", "c"], ["d"]]

a connects to b through x. b connects to c through y. The account d shares no attribute with the other accounts.

Input: records = [["a", "x"], ["a", "x"], ["x", "y"]]
Output: [["a"], ["x"]]

The repeated pair has no effect. The attribute x in the first record is different from the account identifier x in the last record.

Examples

Example 1
Input
([['a', 'x'], ['b', 'x'], ['b', 'y'], ['c', 'y'], ['d', 'z']],)
Output
[['a', 'b', 'c'], ['d']]
Notes
Accounts bridge attributes transitively.
Example 2
Input
([['a', 'x'], ['a', 'x'], ['x', 'y']],)
Output
[['a'], ['x']]
Notes
Account and attribute identifiers occupy separate namespaces.

Constraints

  • •`0 <= len(records) <= 100000`.
  • •Each record contains exactly two strings.
  • •Each string contains 1 to 30 lowercase ASCII letters or digits.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def group_accounts(records: list[list[str]]) -> list[list[str]]:
Sample call
group_accounts([['a', 'x'], ['b', 'x'], ['b', 'y'], ['c', 'y'], ['d', 'z']])
Language
Case 1
Input
([['a', 'x'], ['b', 'x'], ['b', 'y'], ['c', 'y'], ['d', 'z']],)
Expected
[['a', 'b', 'c'], ['d']]
Case 2
Input
([['a', 'x'], ['a', 'x'], ['x', 'y']],)
Expected
[['a'], ['x']]

Browse More Questions

Show the approach

Approach

Create one graph node per account and keep the first account seen for each attribute. For every later use of the attribute, connect its account to that anchor. The resulting star keeps all accounts with the same attribute reachable without creating a quadratic clique. Use separate maps for accounts and attributes.

Search from each unvisited account and collect its reachable component. An edge always connects accounts sharing an attribute, so any graph path represents a valid transitive chain. Conversely, any shared-attribute step has a route through its anchor, so the graph search finds all required connections. Sort each group and then order groups by their first ID.

Time complexity:
O(R + A log A) expected for R records and A accounts, with bounded identifier length
Space complexity:
O(R + A)