Find accessible devices via nested memberships
Company: Axon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of graph traversal and set-based aggregation, with emphasis on handling nested group memberships, cycle-safe expansion, and mapping users to devices.
Constraints
- 0 <= total number of subgroup links + group-user memberships + user-device assignments <= 200000
- The group graph may contain cycles and duplicate references
- All IDs are strings
- Your traversal should be near-linear in the size of the reachable input
Examples
Input: ({'G1': ['G2', 'G3'], 'G2': [], 'G3': []}, {'G1': ['U1'], 'G2': ['U2'], 'G3': ['U3']}, {'U1': ['D1'], 'U2': ['D2', 'D3'], 'U3': ['D4']}, ['G1'])
Expected Output: ['D1', 'D2', 'D3', 'D4']
Explanation: Starting from G1 reaches G1, G2, and G3. Their users are U1, U2, and U3, whose devices combine into D1, D2, D3, and D4.
Input: ({'A': ['B'], 'B': ['C'], 'C': ['A']}, {'A': ['U1'], 'B': ['U2', 'U1'], 'C': ['U3']}, {'U1': ['X1', 'X2'], 'U2': ['X2', 'X3'], 'U3': []}, ['A'])
Expected Output: ['X1', 'X2', 'X3']
Explanation: The groups form a cycle A -> B -> C -> A, but each group is visited once. U1 appears in two groups and X2 appears twice across users, so duplicates are removed.
Input: ({'G1': ['G2'], 'G4': ['G5']}, {'G2': ['U2'], 'G4': ['U4'], 'G5': ['U5']}, {'U2': ['D2'], 'U4': ['D4'], 'U5': ['D5']}, ['G1', 'G4', 'Missing'])
Expected Output: ['D2', 'D4', 'D5']
Explanation: G1 reaches G2 and contributes D2. G4 reaches itself and G5, contributing D4 and D5. The missing start group contributes nothing.
Input: ({}, {}, {}, [])
Expected Output: []
Explanation: With no groups, users, devices, or starting points, no devices are accessible.
Input: ({'G1': ['G1', 'G2', 'G2'], 'G2': []}, {'G1': ['U1', 'U1'], 'G2': ['U2']}, {'U1': ['D1', 'D1'], 'U2': ['D2']}, ['G1', 'G1'])
Expected Output: ['D1', 'D2']
Explanation: Self-references, repeated child links, repeated users, repeated devices, and repeated start groups should all be handled without duplicate output.
Hints
- Model the groups as a directed graph and traverse from all startGroups using DFS or BFS.
- Use visited sets so you do not process the same group or user more than once, especially when cycles or duplicate memberships exist.