KatInterview

Net Access Letters Across a Dependency Hierarchy

Trees & GraphsAuto-graded · 18 test cases
MediumAuto-gradedTrees & Graphsgraphdagtopological-sort

Last updated

Compute each node's final letter set in a DAG where grants flow to all descendants but revocations apply only to the node that declares them.

Net Access Letters Across a Hierarchy

A permission system is modeled as a directed acyclic graph. Each node is an entity, and a directed edge (parent, child) means the child inherits access from the parent.

Every node can carry two optional strings of lowercase letters:

  • Grants: letters this node hands out. A grant applies to the node itself and to every node reachable from it by following edges (all of its descendants).
  • Revocations: letters this node removes from its own final result only. A revocation is never passed on, and it does not stop descendants from receiving the same letter through a grant made by this node or by any of its ancestors.

Rules

  1. A letter is available to node v if v grants it, or if any ancestor of v (a node with a path to v) grants it.
  2. The net permissions of v are the letters available to v, minus the letters in v's own revocations.
  3. Revocations affect only the node that declares them. Even if a parent loses a letter by revocation, a child still has that letter available if it was granted anywhere upstream.

Task

Given the graph and the grant/revocation tables, return the net permissions of every node.

Return a list of (node, permissions) pairs:

  • sorted in ascending order by the node identifier, using ordinary string comparison (so "B10" comes before "B2"),
  • where each permissions string contains the net letters, unique and in alphabetical order,
  • with an empty string "" for a node whose net set is empty (it must still appear in the output).

Input

  • nodes: list of unique node identifier strings.
  • edges: list of (parent, child) pairs. Both endpoints appear in nodes. Duplicate edges may occur.
  • grants: dict mapping a node identifier to a string of unique lowercase letters. A node missing from the dict has no grants.
  • revocations: dict mapping a node identifier to a string of unique lowercase letters. A node missing from the dict has no revocations.

Output

A list of (node, permissions) pairs as described above.

Constraints

  • 1 <= len(nodes) <= 10^4
  • 0 <= len(edges) <= 2 * 10^4
  • Node identifiers are non-empty alphanumeric strings.
  • The graph is guaranteed to be acyclic.
  • All permission characters are lowercase English letters 'a' to 'z'.

Examples

Example 1

Input: compute_net_permissions(['A', 'B', 'C'], [['A', 'B'], ['B', 'C']], {'A': 'rw', 'B': 'x'}, {'B': 'w'})

Output: [['A', 'rw'], ['B', 'rx'], ['C', 'rwx']]

Example 2

Input: compute_net_permissions(['U', 'V', 'W'], [['U', 'W'], ['V', 'W']], {'U': 'a', 'V': 'b', 'W': 'c'}, {'W': 'b'})

Output: [['U', 'a'], ['V', 'b'], ['W', 'ac']]

Hints

Hint 1

Keep two different quantities per node: the letters available through grants (which flow downward) and the final answer (which does not flow anywhere).

Hint 2

Process nodes in topological order so that every parent's available set is complete before it is merged into a child. Kahn's algorithm avoids recursion limits on long chains.

Hint 3

Represent each letter set as a 26-bit integer. Union is OR, removal is AND with the complement, and you can read off the letters from bit 0 to 25 to get alphabetical order.