KatInterview

Letter Access Sets Through a Dependency Graph

Trees & GraphsAuto-graded · 23 test cases
MediumAuto-gradedTrees & Graphstopological-sortbit-manipulationgraph

Last updated

Tests topological processing of a DAG where each node computes an active letter set from its parents' resolved sets plus its own additions and removals.

You are building an access control system modeled as a directed acyclic graph of n nodes numbered 0 to n - 1. Each letter of the lowercase alphabet represents one type of access.

A directed edge [u, v] means node u is a predecessor of node v, and v receives letters from u. On top of what it receives, each node i carries two letter strings:

  • grant[i]: letters this node adds to its active set.
  • block[i]: letters this node removes from its active set.

Computing a node's active set

  1. Collect the active sets of every direct predecessor and take their union. If the node has no predecessors, this union is the empty set.
  2. Add every letter in grant[i].
  3. Remove every letter in block[i].

The result is the node's active set. This is the only thing the node forwards to its successors — its own grant and block strings are not passed on. A letter that was removed at some earlier node can therefore reappear at a later one, either because a different predecessor still carries it, or because the later node adds it through its own grant.

Return the active set of every node as a list of n strings, where element i represents node i's active set. Each string must contain the letters in ascending alphabetical order with no duplicates. A node whose active set is empty maps to "".

Note: node indices do not necessarily appear in topological order, so a predecessor may carry a higher index than its successor.

Constraints

  • 1 <= n <= 100 000
  • 0 <= len(edges) <= 200 000
  • Each edge is [u, v] with 0 <= u, v <= n - 1, u != v. No edge appears more than once.
  • The graph is guaranteed to contain no directed cycle.
  • len(grant) == len(block) == n
  • Each grant[i] and block[i] is a string of 0 to 26 distinct lowercase English letters in any order.
  • For every node i, grant[i] and block[i] share no letter.

Examples

Example 1

Input: compute_active_sets(4, [[0, 1], [0, 2], [1, 3], [2, 3]], ['ab', 'c', '', 'd'], ['', 'a', 'b', ''])

Output: ['ab', 'bc', 'a', 'abcd']

Example 2

Input: compute_active_sets(3, [[0, 1], [1, 2]], ['xy', '', 'x'], ['', 'xy', ''])

Output: ['xy', '', 'x']

Example 3

Input: compute_active_sets(3, [[2, 0], [1, 0]], ['m', 'qp', 'r'], ['q', '', ''])

Output: ['mpr', 'pq', 'r']

Hints

Hint 1

A node's active set can only be finalized once every one of its predecessors has been finalized. What classic graph algorithm visits nodes in exactly that order?

Hint 2

There are at most 26 distinct letters, so any active set fits inside a single integer as a bitmask. Union becomes bitwise OR, adding letters becomes OR, and removing letters becomes AND with a bitwise complement.

Hint 3

When you finalize a node's bitmask, push that single integer to each of its successors — not the grant/block rules. Track a running OR of received masks per node so you can finalize it the moment its last predecessor is done.