FastPrepAccount Roles With Parent-User Inheritance
Problems/Account Roles With Parent-User Inheritance

Account Roles With Parent-User Inheritance

Stripe logoStripe● MediumFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: In a Stripe phone screen, the candidate had a four-part access-control question. Users have accounts and each account has roles; the first two parts were simple loops and are not described. In the third part a user can have a parent user whose roles they inherit, and the task was to list all of a user's roles. The fourth part, the trickiest one, asked which users can access a given set of roles.

The report does not give the input format, how far inheritance reaches, whether the fourth part needs any or all of the roles, or any examples or limits. Here, roles are inherited from every ancestor (not only the direct parent) and never flow up to a parent, the fourth part returns users who hold all of the given roles, both answers are sorted lists without repeats, all questions come as one list of queries, and the formats, limits and examples are ours. The first two parts are covered only by the roles a user gets from their own accounts.

The judged task matches the report at about 70%.

Users own accounts, and roles are granted on accounts. You are given three lists:

  • accounts: each entry "accountId,userId" says that the user owns the account. Every account has exactly one owner; a user may own several accounts or none.
  • roles: each entry "accountId,role" grants the role on that account. A user directly holds every role granted on any account they own.
  • parents: each entry "userId,parentUserId" gives a user's parent user. A user has at most one parent, and the links never form a cycle.

A user holds a role if the user or any ancestor (the parent, the parent's parent, and so on) directly holds it. Roles pass from parents to children only, never from a child to its parent. The users of the system are everyone who appears in accounts or parents.

Answer each query in queries, in order:

  • "roles u": every role that user u holds.
  • "users r1 r2 ...": every user who holds all of the listed roles.

Each answer lists every value once, sorted in increasing character order. Return the answers as a list with one entry per query.

Function

answerAccessQueries(accounts: String[], roles: String[], parents: String[], queries: String[]) → String[][]

Examples

Example 1

accounts = ["a1,alice","a2,alice","b1,bob","c1,carol"]roles = ["a1,admin","a2,viewer","b1,editor","c1,viewer"]parents = ["bob,alice","dave,bob"]queries = ["roles alice","roles bob","roles dave","users viewer","users admin editor"]return = [["admin","viewer"],["admin","editor","viewer"],["admin","editor","viewer"],["alice","bob","carol","dave"],["bob","dave"]]
  • alice owns a1 and a2, so her roles are admin and viewer.
  • bob's parent is alice. He has editor from b1 and inherits admin and viewer.
  • dave owns no account, but his parent is bob, so he inherits everything bob has: admin, editor, viewer.
  • viewer is held by alice, bob, carol and dave.
  • Only bob and dave hold both admin and editor.

Example 2

accounts = ["p1,parent","k1,kid"]roles = ["p1,billing","k1,support"]parents = ["kid,parent"]queries = ["roles parent","roles kid","users support","users billing support","users owner"]return = [["billing"],["billing","support"],["kid"],["kid"],[]]

Roles flow from a parent to its child, never back up: parent keeps only billing, while kid has support plus the inherited billing. So support is held only by kid, and kid is also the only user with both billing and support. Nobody holds owner, so that answer is empty.

Example 3

accounts = ["x1,u3","x2,u3","y1,u1"]roles = ["x1,read","x2,read","y1,write"]parents = ["u2,u1","u3,u2"]queries = ["roles u3","users read write"]return = [["read","write"],["u3"]]

u3's parent is u2, whose parent is u1. Roles are inherited through every ancestor, so u3 gets write from its grandparent u1. read is granted on two of u3's accounts but is listed once. u3 is the only user with both roles.

Constraints

  • 1 <= accounts.length <= 300
  • 1 <= roles.length <= 600
  • 0 <= parents.length <= 199
  • 1 <= queries.length <= 100
  • Every account ID, user ID and role has 1 to 10 characters, each a lowercase English letter or a digit.
  • Account IDs in accounts are distinct, and every account named in roles appears in accounts.
  • In parents, no user is its own parent, no user has two parents, the links form no cycle, and there are at most 200 distinct users in total.
  • Each query is "roles u" for a user u of the system, or "users" followed by 1 to 5 distinct roles, separated by single spaces.

More Stripe problems

  • Portfolio Valuation, Part 1PHONE SCREEN · Seen Nov 2026
  • Chat Billing CalculationOA · Seen Nov 2026
  • Deployment Window SchedulerOA · Seen Nov 2026
All Stripe interview questionsSee Stripe hiring insights
Saved
public String[][] answerAccessQueries(String[] accounts, String[] roles, String[] parents, String[] queries) {
    // Write your code here.
}
Checking account…