Implement a role-based access manager (RBAC) with roles, permissions and user role assignments

Quick Overview

Implement an access manager for role-based access control, with methods to create roles, grant permissions to a role, assign and revoke roles for users, and check whether a user has a permission. It tests data structure choice, handling the edge cases the API leaves open, and per-method complexity.

Implement a role-based access manager (RBAC) with roles, permissions and user role assignments

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: easy

Interview Round: Other

Design and implement an access management system based on role-based access control (RBAC). It has three entities: `Role`, `Permission` and `User`. - Each role has a unique `name` and a set of permissions (for example `"delete_user"` or `"ban_user"`). - A user can hold several roles, and one role can be shared by many users. - A user has a permission if any of their roles contains that permission. - The system must support creating roles, granting more permissions to an existing role, assigning or revoking roles for a user at any time, and checking whether a user currently has a given permission. Implement these methods: - `AccessManager()`: initialize the role registry and the user-to-role mapping. - `createRole(name: string) -> void`: add a new role. - `grantPermission(role: string, permission: string) -> void`: add the permission to the given role. - `assignRole(user: string, role: string) -> void`: give the role to the user. The call is idempotent: assigning a role the user already holds changes nothing. - `revokeRole(user: string, role: string) -> void`: remove the role from the user. It is a no-op if the user does not hold the role. - `hasPermission(user: string, permission: string) -> boolean`: return `true` if the user currently has the permission, otherwise `false`. For example, this sequence of calls produces the results shown on the right: ```text createRole("admin") createRole("moderator") grantPermission("admin", "delete_user") grantPermission("moderator", "ban_user") assignRole("alice", "admin") assignRole("alice", "moderator") assignRole("bob", "moderator") hasPermission("alice", "delete_user") -> true hasPermission("bob", "delete_user") -> false hasPermission("bob", "ban_user") -> true revokeRole("alice", "admin") hasPermission("alice", "delete_user") -> false hasPermission("alice", "ban_user") -> true grantPermission("moderator", "delete_user") hasPermission("bob", "delete_user") -> true ``` Alice loses `delete_user` when `admin` is revoked but keeps `ban_user` through `moderator`. The last grant reaches Bob right away because he already holds `moderator`. ```hint Where the data lives List the lookups each method needs (a role by name, the roles a user holds, whether a role contains a permission) and pick a structure that answers each one in constant time on average. Think about what happens to existing holders when a role gains a permission after it was assigned. ``` ### Constraints & Assumptions - `1 <= number of roles, number of users <= 10^4` - At most `10^5` calls in total to `createRole`, `grantPermission`, `assignRole`, `revokeRole` and `hasPermission`. - Role names, user identifiers and permission strings are non-empty ASCII strings of length at most 64. ### Clarifying Questions to Ask - What should `createRole` do when a role with that name already exists: raise an error or do nothing? - What should `grantPermission` and `assignRole` do when the role does not exist? - Does a user have to be registered before a role is assigned, or is any user string valid? - Will roles ever be deleted, or permissions removed from a role, in a later version? - Is the class used from a single thread, or can calls arrive concurrently? ### What a Strong Answer Covers - The data structures for the role registry, the user-to-role mapping and each role's permissions, with the reason for each choice. - A stated, consistent behavior for the cases the method list leaves open: duplicate role names, unknown roles in `grantPermission` and `assignRole`, unknown users in `revokeRole` and `hasPermission`. - Idempotent `assignRole` and no-op `revokeRole` without special-case code. - Permissions granted after a role was assigned are visible to the users who already hold it. - Time complexity of each method and total space. ### Follow-up Questions - `hasPermission` is called far more often than the other methods. How would you make it constant time, and what does that cost on the write methods? - How would you add role inheritance, where one role includes all permissions of another? - How would you support deleting a role that users still hold, or removing a permission from a role? - What changes if several threads call the access manager at the same time?

Overview: Implement an access manager for role-based access control, with methods to create roles, grant permissions to a role, assign and revoke roles for users, and check whether a user has a permission. It tests data structure choice, handling the edge cases the API leaves open, and per-method complexity.

Nov 24, 2026
easySoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design and implement an access management system based on role-based access control (RBAC). It has three entities: Role, Permission and User.

  • Each role has a unique name and a set of permissions (for example "delete_user" or "ban_user" ).
  • A user can hold several roles, and one role can be shared by many users.
  • A user has a permission if any of their roles contains that permission.
  • The system must support creating roles, granting more permissions to an existing role, assigning or revoking roles for a user at any time, and checking whether a user currently has a given permission.

Implement these methods:

  • AccessManager() : initialize the role registry and the user-to-role mapping.
  • createRole(name: string) -> void : add a new role.
  • grantPermission(role: string, permission: string) -> void : add the permission to the given role.
  • assignRole(user: string, role: string) -> void : give the role to the user. The call is idempotent: assigning a role the user already holds changes nothing.
  • revokeRole(user: string, role: string) -> void : remove the role from the user. It is a no-op if the user does not hold the role.
  • hasPermission(user: string, permission: string) -> boolean : return true if the user currently has the permission, otherwise false .

For example, this sequence of calls produces the results shown on the right:

createRole("admin")
createRole("moderator")
grantPermission("admin", "delete_user")
grantPermission("moderator", "ban_user")
assignRole("alice", "admin")
assignRole("alice", "moderator")
assignRole("bob", "moderator")
hasPermission("alice", "delete_user")    -> true
hasPermission("bob", "delete_user")      -> false
hasPermission("bob", "ban_user")         -> true
revokeRole("alice", "admin")
hasPermission("alice", "delete_user")    -> false
hasPermission("alice", "ban_user")       -> true
grantPermission("moderator", "delete_user")
hasPermission("bob", "delete_user")      -> true

Alice loses delete_user when admin is revoked but keeps ban_user through moderator. The last grant reaches Bob right away because he already holds moderator.

Constraints & Assumptions

  • 1 <= number of roles, number of users <= 10^4
  • At most 10^5 calls in total to createRole , grantPermission , assignRole , revokeRole and hasPermission .
  • Role names, user identifiers and permission strings are non-empty ASCII strings of length at most 64.

Clarifying Questions to Ask Guidance

  • What should createRole do when a role with that name already exists: raise an error or do nothing?
  • What should grantPermission and assignRole do when the role does not exist?
  • Does a user have to be registered before a role is assigned, or is any user string valid?
  • Will roles ever be deleted, or permissions removed from a role, in a later version?
  • Is the class used from a single thread, or can calls arrive concurrently?

What a Strong Answer Covers Guidance

  • The data structures for the role registry, the user-to-role mapping and each role's permissions, with the reason for each choice.
  • A stated, consistent behavior for the cases the method list leaves open: duplicate role names, unknown roles in grantPermission and assignRole , unknown users in revokeRole and hasPermission .
  • Idempotent assignRole and no-op revokeRole without special-case code.
  • Permissions granted after a role was assigned are visible to the users who already hold it.
  • Time complexity of each method and total space.

Follow-up Questions Guidance

  • hasPermission is called far more often than the other methods. How would you make it constant time, and what does that cost on the write methods?
  • How would you add role inheritance, where one role includes all permissions of another?
  • How would you support deleting a role that users still hold, or removing a permission from a role?
  • What changes if several threads call the access manager at the same time?