Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Start free
Role Privilege Inheritance
00:00
5 left

Role Privilege Inheritance

MediumPython

Problem

In Snowflake role-based access control, a role inherits all privileges assigned to its ancestor roles. Given each role's directly assigned privileges and direct inheritance grants, compute the complete privilege set for every role.

A grant [parent, child] means the child role inherits the parent's privileges. Return one sorted list of unique privilege names per role, including privileges assigned directly to that role and inherited through any number of ancestors.

Formal Specification

Implement calculate_privileges(privileges, grants).

  • privileges is a list of lists of strings, where privileges[i] contains privileges directly assigned to role i.
  • grants is a list of two-element integer lists. Each [parent, child] represents a direct inheritance relationship.
  • Return a list of lists of strings. The result at index i must contain every privilege role i can receive, sorted lexicographically.

The inheritance graph is guaranteed to be a directed acyclic graph, and each role index is valid.

Constraints

  • 1 <= len(privileges) <= 10^4
  • 0 <= len(grants) <= 5 * 10^4
  • Each privilege list contains at most 10^3 strings
  • Privilege names contain printable non-whitespace characters
  • The inheritance graph is acyclic
  • Duplicate direct assignments and duplicate grants are allowed

Function Signature

def calculate_privileges(privileges, grants):
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements are in the Question tab.

Sign up free to run or submit. Log in
Code
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 2
Run your code to see test output here.