Design Hierarchical Permission Checks
PinterestMachine Learning Engineer · Technical ScreenLast updated
This question evaluates understanding of hierarchical access control, tree data structures, and permission inheritance, testing competencies in data modeling and algorithmic reasoning.
You are given a hierarchy of groups represented as a tree, for example World -> Country -> City. Each advertiser can be granted access at any group node. A permission granted at a higher-level node applies to all of its descendants.
Design a class that supports these operations:
grant_access(advertiser_id, group_id): add a direct permission for the advertiser on the given group.revoke_access(advertiser_id, group_id): remove the advertiser's direct permission on the given group.check_access(advertiser_id, group_id): returntrueif the advertiser has access to the group, either because the advertiser was granted permission directly on that node or on any ancestor of that node; otherwise returnfalse.
Assume the group tree structure is already known and each node can access its parent.
Follow-up: How would you implement this so that grant_access and revoke_access are O(1), while check_access is O(depth of the tree)?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.