KatInterview

Subtree Access Rights Simulator for Advertiser Groups

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designtreehash-setsimulation

Last updated

Simulate a log of grant, revoke and check operations on a rooted group tree, where a grant on a node also covers every node beneath it.

Subtree Access Rights

A company organizes its groups into a rooted tree, such as World -> Country -> City. The tree is described by a dictionary parent_of that maps each group name to the name of its parent group. The root maps to None.

Advertisers can receive access rights on any group. A right given on a group also applies to every group below it in the tree.

You are given a log of operations to run in order. The log is split into two parallel lists, ops and args. For each index i, ops[i] is the operation name and args[i] is a two-element list [advertiser, group].

There are three kinds of operation:

  • "grant_access": record a direct right for advertiser on group. Granting a right that already exists changes nothing.
  • "revoke_access": remove the direct right of advertiser on exactly group. If that direct right does not exist, nothing changes. Revoking never touches rights held on other groups, whether ancestors or descendants.
  • "check_access": decide whether advertiser currently has access to group. Access exists if the advertiser holds a direct right on group itself or on any of its ancestors up to the root. Otherwise there is no access.

Return a list with one entry per operation, in the same order. Use None for each grant and revoke, and True or False for each check.

Notes

  • Only direct rights are stored. Whether access is inherited is decided at check time, so revoking a right on a descendant does not remove access that comes from an ancestor.
  • A direct right is either present or absent. Granting twice and revoking once leaves it absent.
  • Every group named in a check appears as a key in parent_of.

Constraints

  • The number of operations is at most about 10^5.
  • The tree has at most about 10^5 groups, and its depth is at most about 10^3.
  • Group and advertiser names are non-empty strings.
  • ops and args have the same length, and each element of args has exactly two entries: advertiser first, group second.
  • Each operation name is one of "grant_access", "revoke_access", "check_access".
  • Grant and revoke should take expected O(1) time, and check should take O(depth) time.

Examples

Example 1

Input: simulate_group_access({'World': None, 'Country': 'World', 'City': 'Country'}, ['grant_access', 'check_access', 'check_access', 'revoke_access', 'check_access'], [['a', 'Country'], ['a', 'City'], ['a', 'World'], ['a', 'Country'], ['a', 'City']])

Output: [None, True, False, None, False]

Example 2

Input: simulate_group_access({'root': None, 'north': 'root', 'south': 'root', 'town': 'north'}, ['grant_access', 'check_access', 'grant_access', 'check_access', 'revoke_access', 'check_access'], [['x', 'north'], ['x', 'town'], ['y', 'root'], ['y', 'south'], ['y', 'root'], ['y', 'south']])

Output: [None, True, None, True, None, False]

Hints

Hint 1

Keep only what was explicitly granted, for example a dictionary from advertiser to a set of groups. Do not copy rights down to descendants.

Hint 2

To answer a check, start at the queried group and move to its parent repeatedly until you pass the root. Stop with True as soon as one visited group is in the advertiser's direct set.

Hint 3

Revoke should just discard the group from the advertiser's set, and it must be harmless if the group or the advertiser is unknown. Inheritance is computed on demand, so overlapping grants never need special handling.