Group accounts connected by shared attributes

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

Amazon logo
Amazon
Nov 29, 2026
hardSoftware EngineerTechnical ScreenCoding & Algorithms
0
0

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.