Inherited Role Privileges

Asked at SnowflakePhone screen

Reported by the problem's creator, not verified

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.

Examples

  1. Example 1

    Input: init = [4,[[0,1],[1,2]]], operations = [["effective",[[["a"],["b"],["c"],[]]]]]

    Output: [[["a"],["a","b"],["a","b","c"],[]]]

    Explanation: Role 1 inherits role 0's a and adds b; role 2 inherits both and adds c. Role 3 has no grants and no privileges.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.