Implement hierarchical folder access check
Problem: Hierarchical Folder Access Check
You are tasked with simulating a simple hierarchical file system and implementing an access-check function. The file system consists of folders arranged in a tree structure.
Access rules are as follows:
- A user is granted direct access to a specific subset of folders.
- Access is inherited down the tree: if a user has access to a parent folder, they automatically have access to all of its descendant folders.
You will be provided with the following inputs:
allFolders: A list of all folders in the system, represented as absolute paths from the root (e.g.,"/","/A","/A/B/C").accessibleFolders: A set of folder paths to which the user has direct access (e.g.,{" /A", "/F"}).
Function to Implement
You need to implement the function:
bool HasAccess(string folderPath)
This function should return:
trueif the user has access tofolderPatheither because:folderPathitself is present inaccessibleFolders, OR- An ancestor folder of
folderPath(e.g.,/Ais an ancestor of/A/B/C) is present inaccessibleFolders.
falseotherwise.
Assumptions
- All folder paths in
allFoldersandaccessibleFoldersare normalized absolute paths, starting with'/', with components separated by'/'(e.g.,/A/B/C). folderPathpassed intoHasAccessis always a valid folder path present inallFolders.
Efficiency Requirement
Design and implement HasAccess so that it can be called many times efficiently after the initial inputs (allFolders and accessibleFolders) are known.
Examples
Given the following folder tree:
/
├── A
│ ├── B
│ │ ├── C
│ │ └── D
│ └── E
└── F
And accessibleFolders = {"/A", "/F"}:
HasAccess("/A")→true(direct access)HasAccess("/A/B")→true(inherits from/A)HasAccess("/A/B/C")→true(inherits from/A)HasAccess("/A/E")→true(inherits from/A)HasAccess("/F")→true(direct access)HasAccess("/")→false(no access to root unless/is inaccessibleFolders)
AI interview prep
Core dilemma
Optimizing for extremely fast, repeated access checks in a hierarchical system by balancing initial setup cost (time and memory) against per-query performance.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement hierarchical folder access check
Problem: Hierarchical Folder Access Check
You are tasked with simulating a simple hierarchical file system and implementing an access-check function. The file system consists of folders arranged in a tree structure.
Access rules are as follows:
- A user is granted direct access to a specific subset of folders.
- Access is inherited down the tree: if a user has access to a parent folder, they automatically have access to all of its descendant folders.
You will be provided with the following inputs:
allFolders: A list of all folders in the system, represented as absolute paths from the root (e.g.,"/","/A","/A/B/C").accessibleFolders: A set of folder paths to which the user has direct access (e.g.,{" /A", "/F"}).
Function to Implement
You need to implement the function:
bool HasAccess(string folderPath)
This function should return:
trueif the user has access tofolderPatheither because:folderPathitself is present inaccessibleFolders, OR- An ancestor folder of
folderPath(e.g.,/Ais an ancestor of/A/B/C) is present inaccessibleFolders.
falseotherwise.
Assumptions
- All folder paths in
allFoldersandaccessibleFoldersare normalized absolute paths, starting with'/', with components separated by'/'(e.g.,/A/B/C). folderPathpassed intoHasAccessis always a valid folder path present inallFolders.
Efficiency Requirement
Design and implement HasAccess so that it can be called many times efficiently after the initial inputs (allFolders and accessibleFolders) are known.
Examples
Given the following folder tree:
/
├── A
│ ├── B
│ │ ├── C
│ │ └── D
│ └── E
└── F
And accessibleFolders = {"/A", "/F"}:
HasAccess("/A")→true(direct access)HasAccess("/A/B")→true(inherits from/A)HasAccess("/A/B/C")→true(inherits from/A)HasAccess("/A/E")→true(inherits from/A)HasAccess("/F")→true(direct access)HasAccess("/")→false(no access to root unless/is inaccessibleFolders)