Inherited Role Privileges
SnowflakePhone screenMedium
A company models access rules as roles. You are given a class RoleGraph whose constructor takes n, the number of roles numbered 0 to n - 1, and grants, a list of [parent, child] pairs meaning child inherits from parent. The grants form a directed acyclic graph, and a role may have several parents. Inheritance is transitive: a role inherits from its parents' parents too.
Privileges are lowercase strings. effective(allow) takes a list of length n, where allow[i] lists the privileges assigned directly to role i, and returns one list per role.
Each test constructs one RoleGraph and calls effective once; the output lists every call's return value.
Part 1: Inherited privileges
Implement effective(allow). allow[i] lists the privileges assigned directly to role i and may repeat a privilege.
A role's privileges are its own plus everything it inherits through any chain of grants. Return a list with one entry per role: that role's privileges sorted, with repeats removed.
- Example 1
- init
[4,[[0,1],[1,2]]]- operations
[["effective",[[["a"],["b"],["c"],[]]]]]- Output
[[["a"],["a","b"],["a","b","c"],[]]]
Why: Role 1 inherits role 0's
aand addsb; role 2 inherits both and addsc. Role 3 has no grants and no privileges.
Constraints
1 <= n <= 10^40 <= len(grants) <= 2 * 10^40 <= parent < n0 <= child < ngrantshas no cycles- Total length of all
allow[i]is at most2 * 10^4