Problem · Hash Table

Inherited Role Permissions

Learn this problem
MediumGoldman SachsONSITE INTERVIEW

Problem statement

A role may inherit from one parent role. Each role has an allow list and a deny list of permission names. A child role inherits decisions from its ancestors, but the nearest explicit decision overrides every decision higher in the hierarchy.

For the requested role and permission:

  • Starting at role, walk toward the root.
  • If the current role explicitly denies the permission, return false.
  • If the current role explicitly allows the permission, return true.
  • If no role in the chain mentions the permission, return false.

The arrays roles, parents, allowLists, and denyLists are aligned by index. A root role has the empty string as its parent. A role never contains the same permission in both of its own lists.

Function

hasPermission(roles: String[], parents: String[], allowLists: String[][], denyLists: String[][], role: String, permission: String) → boolean

Examples

Example 1

roles = ["employee","engineer","intern"]parents = ["","employee","engineer"]allowLists = [["read"],["deploy"],[]]denyLists = [[],[],["deploy"]]role = "intern"permission = "deploy"return = false

The intern's explicit deny overrides the engineer's inherited allow.

Example 2

roles = ["employee","engineer","intern"]parents = ["","employee","engineer"]allowLists = [["read"],["deploy"],[]]denyLists = [[],[],["deploy"]]role = "intern"permission = "read"return = true

Neither intern nor engineer mentions read, so the permission is inherited from employee.

More Goldman Sachs problems

drafts saved locally
public boolean hasPermission(String[] roles, String[] parents, String[][] allowLists, String[][] denyLists, String role, String permission) {
  // write your code here
}
roles["employee","engineer","intern"]
parents["","employee","engineer"]
allowLists[["read"],["deploy"],[]]
denyLists[[],[],["deploy"]]
role"intern"
permission"deploy"
expectedfalse
checking account