Compute Inherited Role Privileges
Company: Snowflake
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of directed acyclic graph traversal, transitive inheritance, and set aggregation/deduplication, assessing competency in graph algorithms, data structures, and permission-model reasoning.
Constraints
- 0 <= n == len(initialPrivileges) <= 10^4
- 0 <= len(grants) <= 2 * 10^4
- 0 <= parent, child < n for every [parent, child] in grants
- The grant graph is a directed acyclic graph
- The total number of directly listed privileges across all roles is at most 2 * 10^4
Examples
Input: ([['A'], ['B'], ['C']], [[0, 1], [1, 2]])
Expected Output: [['A'], ['A', 'B'], ['A', 'B', 'C']]
Explanation: Role 1 inherits A from role 0. Role 2 inherits from role 1, so it gets both A and B in addition to its own C.
Input: ([['read', 'write'], ['write', 'write'], ['execute'], []], [[0, 3], [1, 3], [2, 3]])
Expected Output: [['read', 'write'], ['write'], ['execute'], ['execute', 'read', 'write']]
Explanation: Role 3 inherits privileges from three parents. Duplicate privileges such as write appear only once in the final result.
Input: ([['admin'], [], ['audit'], ['user']], [[0, 1], [1, 2]])
Expected Output: [['admin'], ['admin'], ['admin', 'audit'], ['user']]
Explanation: Role 1 inherits admin from role 0, and role 2 inherits from role 1 transitively. Role 3 is disconnected and keeps only its own privilege.
Input: ([], [])
Expected Output: []
Explanation: There are no roles, so the result is an empty list.
Hints
- Because the inheritance graph is a DAG, think about processing roles in topological order so that a parent is handled before its children.
- Use a set for each role to merge privileges efficiently and automatically remove duplicates.