Problem · Tree

RBAC Role Resolver - User Roles with Inheritance

Learn this problem
MediumStripe logoStripeFULLTIMEPHONE SCREEN
See Stripe hiring insights

Problem statement

You are building a Role-Based Access Control (RBAC) system over a hierarchy of accounts (a tree: Organization → Workspace → Team). Resolve all roles a user has on a given account, including roles inherited from ancestor accounts.

Inputs:

  • accounts: a String[] where each entry is "accountId,parent"; a root account uses the literal null as its parent. Each account has exactly one parent and there are no cycles.
  • assignments: a String[] where each entry is "userId,accountId,role" assigning a role to a user on a specific account.
  • userId and accountId: the query.

Inheritance: a user's roles on an account are the union of roles assigned to that user on the account itself and on every ancestor (parent, grandparent, ...). For example, an admin at the organization is an admin on every workspace and team beneath it.

Return the user's roles on accountId as a String[] sorted alphabetically (no duplicates). Return an empty array if the user has no roles there.

Function

getUserRoles(accounts: String[], assignments: String[], userId: String, accountId: String) → String[]

Examples

Example 1

accounts = ["org_1,null", "wksp_1,org_1", "team_1,wksp_1"]assignments = ["usr_1,org_1,admin", "usr_1,wksp_1,editor", "usr_2,wksp_1,viewer"]userId = "usr_1"accountId = "wksp_1"return = ["admin", "editor"]
usr_1 is editor directly on wksp_1 and admin on the parent org_1, which is inherited down to wksp_1. The union sorted alphabetically is [admin, editor].

Example 2

accounts = ["org_1,null", "wksp_1,org_1", "team_1,wksp_1"]assignments = ["usr_1,org_1,admin", "usr_1,wksp_1,editor", "usr_2,wksp_1,viewer"]userId = "usr_2"accountId = "team_1"return = ["viewer"]
usr_2 is viewer on wksp_1; team_1's parent chain is team_1 -> wksp_1 -> org_1, so the viewer role is inherited down to team_1. usr_2 has no role on org_1.

Example 3

accounts = ["org_1,null", "wksp_1,org_1", "team_1,wksp_1"]assignments = ["usr_1,org_1,admin", "usr_1,wksp_1,editor", "usr_2,wksp_1,viewer"]userId = "usr_2"accountId = "org_1"return = []
Roles inherit downward only. usr_2's role is on wksp_1, a child of org_1, so it does not propagate up to org_1. usr_2 has no roles at the top level.

Constraints

  • Each account entry is "accountId,parent"; a root uses null as parent. One parent per account, no cycles.
  • Each assignment is "userId,accountId,role".
  • Roles inherit downward: a user's roles on an account are the union over the account and all its ancestors.
  • Return the alphabetically-sorted, de-duplicated String[] of roles; empty if none.

More Stripe problems

drafts saved locally
public String[] getUserRoles(String[] accounts, String[] assignments, String userId, String accountId) {
  // write your code here
}
accounts["org_1,null", "wksp_1,org_1", "team_1,wksp_1"]
assignments["usr_1,org_1,admin", "usr_1,wksp_1,editor", "usr_2,wksp_1,viewer"]
userId"usr_1"
accountId"wksp_1"
expected["admin", "editor"]
checking account