← Snowflake Interview Insights
The base case is straightforward, just union the direct privileges.
Model the roles and inheritance as a directed acyclic graph (DAG) where each node represents a role and edges represent parent-child grant relationships. Use topological sorting to process roles in dependency order, computing each role's privilege set as the union of its directly assigned privileges and the privilege sets of its parents. Deduplicate privileges using a set data structure for each role.
Pro tip: Mention that you can optimize memory by using bitsets for privilege sets if the number of distinct privileges is small, and that handling cycles is unnecessary because the input is guaranteed to be a DAG. Also, clarify whether the inheritance is from parent to child (child inherits parent's privileges) or vice versa, as this affects the direction of edges.
Confirm the direction of inheritance (e.g., child roles inherit privileges from parent roles) and that the graph is a DAG. Ask about constraints like number of roles, privileges, and edges to choose the right algorithm.
Build an adjacency list for the DAG and initialize each role's privilege set with its directly assigned privileges. Use a set (or bitset) to ensure deduplication.
Perform a topological sort on the DAG to determine a linear order where each role appears after all its parents. This ensures that when processing a role, all its parents' privileges are already computed.
Iterate through the sorted roles, and for each role, union its privilege set with the privilege sets of all its parents. This propagates privileges transitively.
After processing all roles, return the privilege set for each role, ensuring no duplicates. Optionally, convert sets to sorted lists for consistent output.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.