Katkat

Design Hierarchical Permission Checks

PinterestMachine Learning Engineer · Technical Screen

Last 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): return true if 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 return false.

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)?