KatInterview

Inherited Permissions Across a Group Hierarchy

Trees & GraphsAuto-graded · 21 test cases
MediumAuto-gradedTrees & Graphsgraphdagdfs

Last updated

Answer permission queries over a DAG of groups where each group inherits from its parents, using graph traversal with memoization or caching.

Inherited Permissions Across a Group Hierarchy

A platform organizes its user groups into a hierarchy shaped like a directed acyclic graph (DAG). A group automatically gets every permission held by any of its parent groups, and this carries up through any number of levels.

You are given three inputs:

  • edges: a list of [child, parent] pairs. Each pair means child inherits from parent. A group may appear in several pairs, so it can have multiple parents and multiple children.
  • direct_grants: a list of [group, permission] pairs. Each pair means group was explicitly given permission.
  • queries: a list of operations. Each operation is a list of strings whose first element is the operation name.
    • An operation of the form ["has_permission", group, permission] is a check.
    • Any operation whose name is not "has_permission" must be ignored. It produces no output.

A group holds a permission if either:

  1. the permission was granted directly to that group, or
  2. at least one of its parents holds the permission (recursively, so grandparents and higher ancestors count).

Task

Implement a function that processes queries in order and returns a list of booleans, one for each "has_permission" operation, in the same order. Each value is True if the given group holds the given permission, and False otherwise.

Rules

  • A group or permission that never appears in the inputs is valid. Such a group has no parents and no grants, so the answer is False.
  • Permissions flow only upward from parent to child. A parent never receives permissions granted to its children.
  • Groups in separate, disconnected parts of the hierarchy do not affect each other.
  • The hierarchy is guaranteed to contain no cycles.

Constraints

  • 0 <= len(edges) <= 10,000
  • 0 <= len(direct_grants) <= 5,000
  • The graph formed by edges is a DAG.
  • Group and permission names are strings.
  • Chains of inheritance can be long, so make sure deep hierarchies do not crash your solution.

Examples

Example 1

Input: resolve_permissions([['admin', 'user'], ['manager', 'user']], [['user', 'view_ads']], [['has_permission', 'admin', 'view_ads'], ['has_permission', 'manager', 'edit_campaign']])

Output: [True, False]

Example 2

Input: resolve_permissions([['campaign_manager', 'manager'], ['manager', 'advertiser'], ['advertiser', 'user']], [['user', 'view_ads'], ['advertiser', 'edit_campaign']], [['has_permission', 'campaign_manager', 'view_ads'], ['has_permission', 'campaign_manager', 'edit_campaign'], ['has_permission', 'user', 'edit_campaign']])

Output: [True, True, False]

Example 3

Input: resolve_permissions([['a', 'root'], ['b', 'root'], ['c', 'a'], ['c', 'b']], [['root', 'read'], ['b', 'write']], [['has_permission', 'c', 'read'], ['has_permission', 'c', 'write'], ['has_permission', 'a', 'write']])

Output: [True, True, False]

Hints

Hint 1

Queries only ever move from a group toward its ancestors, so store each group's list of parents rather than its children.

Hint 2

A group holds a permission if it was granted directly, or if any parent holds it. Keep the direct grants in a hash set of (group, permission) pairs for constant-time lookup.

Hint 3

Diamond shapes let many paths reach the same ancestor. Cache the result for each (group, permission) pair, or use a visited set per query, so no state is explored twice. Consider an explicit stack if the chain could be very deep.