Inherited permissions across teams, folders and files
I was asked this
No reports yet. Sign in to say where you were asked it.
Design a permission system for a workspace organized as a hierarchy of teams, folders, and files.
- A team is a root. It can contain folders and files.
- A folder sits inside a team or another folder. It can contain folders and files.
- A file is a leaf. It lives inside a team or a folder and has no children.
Every entity has a unique string uuid and a list of user IDs who were given access to it directly. Access is inherited: a user who has direct access to an entity can access every entity below it, at any depth. Access never flows upward.
Implement a class PermissionSystem with:
add_team(uuid, user_ids)add_folder(uuid, parent_uuid, user_ids)add_file(uuid, parent_uuid, user_ids)-
has_access(user_id, uuid) -> bool: can this user open this entity? -
accessible_files(user_id) -> list[str]: the IDs of all files the user can open, sorted, each listed once.
Raise an error for a duplicate ID, an unknown parent, or a file used as a parent. Assume up to 10^5 entities and trees that can be deep.
Example
Team studio (users: mia) contains folder brand (users: raj) and file budget (no users). Folder brand contains file palette (users: lee).
-
has_access("mia", "palette")->True(inherited fromstudio) -
has_access("raj", "budget")->False(budgetis not underbrand) -
has_access("lee", "brand")->False(access does not flow upward) -
accessible_files("mia")->["budget", "palette"] -
accessible_files("raj")->["palette"]
Reconstructed from the reported question; details may differ from what interviewers ask.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
Sign in to submit your code; every attempt is kept here.
