Propagating Allow/Deny Permissions Through an Inheritance DAG
Trees & GraphsModel answerLast 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
- Ask (or explicitly state) your conflict-resolution assumptions before writing any code.
- Process the graph in an order that guarantees each node's parents are fully resolved before the node itself is computed.
- Detect cycles in the input rather than looping or silently producing wrong results.
- Return a mapping from every node name to its final set of permitted permissions.
- 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.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Model Answer
Step 0 — Settle the conflict rules first
There is no universally correct answer; what matters is that you pick a rule, state it clearly, and implement it without contradictions. A reasonable default (and the one used in this solution) is:
Deny beats allow everywhere. If a permission appears in any deny list — whether on the node itself or anywhere in its ancestry — it is denied. An allow only takes effect if no deny exists for that permission in the entire inherited chain.
Secondary rules that follow:
- A permission in both lists on the same node is denied (deny wins locally too).
- A permission mentioned by neither any allow nor any deny list anywhere in the chain is denied by default (closed/allowlist model).
- When two parents disagree, the deny-side parent wins.
If the interviewer prefers "nearest explicit rule wins" or "explicit local rule overrides inherited deny," the merge function changes but the graph traversal does not.
Step 1 — Topological processing
Use Kahn's algorithm (BFS with in-degree tracking). It processes every node only after all its parents are done, which is exactly what inheritance requires. It also detects cycles for free: if the BFS queue empties before every node has been visited, the unvisited nodes form a cycle.
Step 2 — The merge function
For each node, once all parents are resolved:
inherited_allow = union of all parent effective_allow sets
inherited_deny = union of all parent effective_deny sets
# Merge with the node's own lists
combined_allow = inherited_allow ∪ node.allow
combined_deny = inherited_deny ∪ node.deny
# Apply the conflict rule: deny beats allow
effective_allow = combined_allow − combined_deny
effective_deny = combined_deny
Keeping both sets through the traversal (not just the final allowed set) is important so that deny information keeps propagating to children.
Full implementation
from collections import defaultdict, deque
def effective_permissions(nodes, edges):
# Build adjacency structures
children = defaultdict(list) # parent -> [child]
in_degree = {n: 0 for n in nodes}
for parent, child in edges:
children[parent].append(child)
in_degree[child] += 1
# effective[node] = (allow_set, deny_set) after full inheritance
effective = {}
# Seed each node's own lists
own = {
node: (set(data["allow"]), set(data["deny"]))
for node, data in nodes.items()
}
# Kahn's BFS
queue = deque(node for node, deg in in_degree.items() if deg == 0)
# Track inherited sets before merging with own rules
inherited_allow = defaultdict(set)
inherited_deny = defaultdict(set)
while queue:
node = queue.popleft()
# Merge inherited sets with the node's own lists
combined_allow = inherited_allow[node] | own[node][0]
combined_deny = inherited_deny[node] | own[node][1]
# Deny beats allow
final_allow = combined_allow - combined_deny
final_deny = combined_deny
effective[node] = final_allow # callers only need the allowed set
# Propagate to children
for child in children[node]:
inherited_allow[child] |= final_allow
inherited_deny[child] |= final_deny
in_degree[child] -= 1
if in_degree[child] == 0:
queue.append(child)
# Cycle detection
if len(effective) != len(nodes):
cycle_nodes = [n for n in nodes if n not in effective]
raise ValueError(f"Cycle detected involving nodes: {cycle_nodes}")
return effective
Worked example output under deny-beats-allow:
| Node | Inherited allow | Inherited deny | Own allow | Own deny | Final |
|---|---|---|---|---|---|
| org | — | — | read, write | — | {read, write} |
| eng | read, write | — | deploy | write | {read, deploy} |
| finance | read, write | — | — | deploy | {read, write} |
| payroll | read, deploy, read, write | deploy | write | — | {read} |
payroll ends up with only read because deploy is denied by finance and write is denied by eng, and both denials propagate.
Complexity
- Time: O(N + E + N·P) where N = nodes, E = edges, P = distinct permissions. The set operations at each node are O(P).
- Space: O(N·P) to store the effective sets.
- Optimization: represent permission sets as bitmasks (one bit per distinct permission). Set union becomes bitwise OR, subtraction becomes
allow & ~deny. This turns O(P) set operations into O(1) integer operations and makes the algorithm cache-friendly at scale.
Common mistakes
- Propagating only the final allowed set, not the deny set. If you drop the deny information after computing a node's effective permissions, children cannot inherit the denial. The deny set must travel through the graph independently.
- Processing in arbitrary order. Any node processed before all its parents are ready will inherit an incomplete picture. Topological order is mandatory, not optional.
- Silently swallowing cycles. A cycle means the BFS queue empties early. Check
len(effective) == len(nodes)and raise an error rather than returning a partial result. - Not stating the conflict rule. An implementation that never addresses the ambiguity is incomplete. The rule choice belongs in comments or documentation, not just in an interviewer's head.
Follow-up: incremental updates
If edges or permission lists change frequently, full recomputation is wasteful. A practical approach is to mark a changed node and all its descendants (reachable via BFS/DFS on the forward graph) as dirty, then recompute only those nodes in topological order. This makes updates O(D·P) where D is the number of dirty nodes, which is much cheaper when changes are localized near leaves.