KatInterview

Propagating Allow/Deny Permissions Through an Inheritance DAG

Trees & GraphsModel answer
MediumModel answerTrees & Graphsgraphstopological-sortpermissions

Last updated

Given a DAG where each node carries allow and deny lists, compute every node's effective permissions by inheriting and merging rules along edges in topological order.

Propagating Allow/Deny Permissions Through an Inheritance DAG

You are building the authorization layer for a hierarchical system — think organization units, folder trees, or role hierarchies. Each node in the hierarchy holds two lists: an allow list and a deny list of named permissions. A node also inherits the permissions of every node that points into it.

Given a directed acyclic graph and the allow/deny lists for every node, return the effective permission set for every node after inheritance is fully resolved.

nodes = {
    "org":     {"allow": ["read", "write"], "deny": []},
    "eng":     {"allow": ["deploy"],        "deny": ["write"]},
    "finance": {"allow": [],                "deny": ["deploy"]},
    "payroll": {"allow": ["write"],         "deny": []},
}
edges = [
    ("org", "eng"),
    ("org", "finance"),
    ("eng", "payroll"),
    ("finance", "payroll"),
]

effective_permissions(nodes, edges)
# -> {node: set of permission strings}

An edge (a, b) means b inherits from a.

In the example, payroll inherits from both eng and finance. eng allows deploy while finance denies it. eng denies write while payroll's own list allows it. What payroll ends up with depends entirely on the conflict-resolution rule you choose — and that rule is not specified. Part of the problem is to identify the ambiguities, state the rule you are adopting, and implement it consistently.

What is expected

  1. Ask (or explicitly state) your conflict-resolution assumptions before writing any code.
  2. Process the graph in an order that guarantees each node's parents are fully resolved before the node itself is computed.
  3. Detect cycles in the input rather than looping or silently producing wrong results.
  4. Return a mapping from every node name to its final set of permitted permissions.
  5. Discuss the time and space complexity in terms of nodes, edges, and the number of distinct permissions.

Constraints

  • The graph is directed and acyclic under normal input; handle the cycle-as-error case.
  • Node and permission names are non-empty strings.
  • A node with no incoming edges inherits nothing; its effective permissions come from its own lists only.
  • Graph size is unspecified — ask if it matters for your implementation choices.

Open questions you should address

  • When a node's own allow list conflicts with a deny inherited from a parent, which wins?
  • When two parents disagree on the same permission, which wins — and does the answer change if one conflict is local and the other is inherited?
  • What happens if a single node lists the same permission in both its allow and deny lists?
  • Is an unmentioned permission granted or denied by default?
  • Should results be computed for all nodes up front, or fetched on demand with possible incremental updates?

Hints

Hint 1

Before touching the graph, nail down the conflict rule. A common choice is: deny always beats allow, regardless of where each comes from. Another is: a node's own explicit rule overrides anything inherited. Write the rule down as a sentence and code exactly that sentence.

Hint 2

Topological sort (Kahn's algorithm with in-degree counts, or DFS post-order) guarantees you process every parent before any of its children. Once a node's parents are all resolved, merge their effective sets with the node's own lists in one pass.

Hint 3

Cycle detection is a natural byproduct of Kahn's algorithm: if the queue empties before all nodes are processed, at least one cycle exists. Track which nodes were never dequeued to report the culprits.