Group accounts connected by shared attributes
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
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
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
- Sharing one attribute connects all accounts using it, even if only some edges are kept.
- Account IDs and attributes are independent kinds of keys.