Design a tree-based permission system with inheritance and explicit overrides
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a tree-based permission system where nodes represent resources organized in a hierarchy. Each node can have permissions set directly on it, or it can inherit permissions from its parent node.
When a permission is explicitly set on a node, it overrides any inherited value for that permission. If a permission is not set on a node, the system walks up the tree to find the nearest ancestor with that permission set. If no ancestor has the permission, the permission is denied (returns `false`).
The tree is initialized with a single root node. Child nodes can be added under any existing node. Permissions can be set, removed and queried on any node in the tree.
| Method | Returns | Behavior |
|---|---|---|
| `AccessControlTree(root_name: str)` | constructor | Creates a tree with a single root node named `root_name`. |
| `add_child(parent_name: str, child_name: str)` | `bool` | Adds a child node under the given parent. Returns `True` on success, `False` if the parent does not exist or the child name already exists. |
| `set_permission(node_name: str, permission: str, granted: bool)` | `bool` | Sets an explicit permission on a node. Returns `True` on success, `False` if the node does not exist. |
| `remove_permission(node_name: str, permission: str)` | `bool` | Removes an explicit permission from a node so it reverts to inheriting from its parent. Returns `True` if the permission was removed, `False` if the node does not exist or the permission was not explicitly set on it. |
| `check_permission(node_name: str, permission: str)` | `bool` | Returns whether the permission is granted for the node, using the nearest explicit setting on the path up to the root. Returns `False` if no node on that path sets it or the node does not exist. |
| `get_all_permissions(node_name: str)` | `List[str]` | Returns every permission that is effectively granted (`True`) for the node once inheritance is applied, sorted ascending. Returns an empty list if the node does not exist. |
Calls are given as a list of operation names and a matching list of argument lists. The output holds `null` for the constructor and the return value of every other call.
Example 1:
```text
Input:
operations = ["AccessControlTree", "add_child", "add_child", "set_permission",
"check_permission", "check_permission", "set_permission",
"check_permission", "remove_permission", "check_permission",
"get_all_permissions"]
arguments = [["root"], ["root", "docs"], ["docs", "secret"], ["root", "read", true],
["docs", "read"], ["secret", "read"], ["secret", "read", false],
["secret", "read"], ["secret", "read"], ["secret", "read"],
["docs"]]
Output: [null, true, true, true, true, true, true, false, true, true, ["read"]]
```
The tree is the chain `root`, `docs`, `secret`. After `read` is granted on `root`, both `docs` and `secret` inherit it. Setting `read` to false on `secret` overrides the inherited grant, so the next check on `secret` returns false. Removing that override makes `secret` inherit `true` again, and the effective permissions of `docs` are `["read"]`.
Example 2:
```text
Input:
operations = ["AccessControlTree", "set_permission", "set_permission", "add_child",
"add_child", "check_permission", "check_permission", "set_permission",
"get_all_permissions", "get_all_permissions"]
arguments = [["company"], ["company", "read", true], ["company", "write", true],
["company", "engineering"], ["engineering", "backend"],
["backend", "read"], ["backend", "write"], ["backend", "write", false],
["backend"], ["engineering"]]
Output: [null, true, true, true, true, true, true, true, ["read"], ["read", "write"]]
```
The root `company` grants `read` and `write`, and `engineering` and `backend` inherit both. After `write` is set to false on `backend`, its effective set is `["read"]`, while `engineering` still has `["read", "write"]`.
```hint An explicit false
A node can hold a permission set to false, or no setting at all for it. Check that your lookup tells these two states apart, since only one of them should stop the walk up the tree.
```
```hint Merge order
For `get_all_permissions`, several nodes on the path to the root can set the same permission. Pick the order in which you combine them so that the setting closest to the node decides.
```
### Constraints & Assumptions
- `1 <= length of node names <= 50`
- `1 <= length of permission names <= 50`
- Node names and permission names consist of lowercase English letters, digits and underscores.
- All node names are unique.
- At most `10⁴` total operations.
### Clarifying Questions to Ask
- Can nodes be deleted or moved to a different parent, or does the tree only grow?
- Does `set_permission` on a node that already sets the same permission simply overwrite the old value?
- What ordering does "sorted" mean for `get_all_permissions`: plain string comparison?
- Which operations dominate the workload? If checks far outnumber writes, caching effective permissions may be worth it.
### What a Strong Answer Covers
- A data model that finds any node by name in constant time and can reach each node's parent.
- A check that stops at the nearest explicit setting, honors an explicit false as an override and defaults to denied.
- An effective-permission computation in which closer settings win, returned in sorted order.
- The exact return value for every failure path: missing parent, duplicate child name, missing node and removing a permission that was never set on that node.
- Time and space complexity per operation, including the cost on deep trees.
### Follow-up Questions
- How would you support deleting a node or moving a subtree under a new parent, and what happens to the permissions below it?
- If checks outnumber writes a thousand to one, how would you make `check_permission` faster, and how would you keep a cache correct after `set_permission` or `remove_permission`?
- How would you add users and groups, so that each setting also names who holds the permission on a node?
- How would the model change if a resource could have more than one parent?
Overview: Design a tree of resources where each node can set permissions directly or inherit them from the nearest ancestor that does, with methods to add children and to set, remove, check and list permissions. It tests data modeling, override rules for explicit denies, and the cost of each operation on deep trees.