File Permission Reachability
FigmaPhone screenMedium
A file-sharing product models access inheritance as a directed graph. Nodes are numbered 0 to n - 1. An edge [a, b] means access held on a is inherited by b. The graph may contain cycles, self loops and repeated edges.
Build a class PermissionGraph(n, edges) that answers reachability queries. A node always reaches itself. Each test creates one graph and then issues every query against it; each query returns a boolean.
Part 1: Directed reachability
Implement reachable(src, dst), which returns True when a path of edges, each followed from a to b, leads from src to dst, and False otherwise.
- Example 1
- init
[3,[[0,1],[1,2]]]- operations
[["reachable",[0,2]],["reachable",[2,0]],["reachable",[0,1]],["reachable",[1,0]],["reachable",[1,1]],["reachable",[2,2]]]- Output
[true,false,true,false,true,true]
Why: Access flows along the edges, so 0 reaches 1 and 2, and 1 reaches 2. Nothing flows backward, so 2 cannot reach 0 and 1 cannot reach 0. Every node reaches itself.
Constraints
1 <= n <= 10^50 <= len(edges) <= 2 * 10^50 <= a, b < nfor every edge[a, b]; self loops and repeated edges may occur1 <= len(operations) <= 10^4