Snowflake questions

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.

  1. 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 a and adds b; role 2 inherits both and adds c. Role 3 has no grants and no privileges.

Constraints

  • 1 <= n <= 10^4
  • 0 <= len(grants) <= 2 * 10^4
  • 0 <= parent < n
  • 0 <= child < n
  • grants has no cycles
  • Total length of all allow[i] is at most 2 * 10^4
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question
Inherited Role Privileges: Snowflake interview question | LoopCoach