Implement timestamp-based access overrides on a static tree of geographic regions. Grants and denials inherit through descendants, later operations win even when they arrive out of order, and each check must find the newest applicable rule on the root-to-region path.
## Prompt
You are given a rooted tree of geographic regions. Implement `grant_access(region, timestamp)`, `deny_access(region, timestamp)`, and `check_access(region)`. A grant or denial at a region applies to that region and its entire subtree. Later operations override earlier operations, including a later operation on a descendant. Explain both a simple implementation and an optimized design for many checks.
### Constraints & Assumptions
- Timestamps are unique and operations may arrive out of timestamp order.
- A region with no applicable operation is denied by default.
- The tree is static after construction.
- A check must reflect the greatest-timestamp operation among the region and all of its ancestors.
### Clarifying Questions to Ask
- Does an older late-arriving operation replace newer state? It should not under timestamp ordering.
- What are the expected ratios of updates to checks and the maximum tree depth?
- Must access checks be linearizable with concurrent updates?
```hint Query the path that matters
A region is controlled only by updates on its root-to-node path; sibling updates are irrelevant.
```
```hint Flatten subtrees when updates dominate
An Euler tour converts every subtree to one contiguous interval, enabling range-update and point-query structures.
```
### What a Strong Answer Covers
- A correct inheritance and last-write-wins rule for ancestor and descendant operations.
- A baseline parent-walk design with explicit time and space complexity.
- An Euler-tour or tree-decomposition optimization suited to the workload.
- Handling for out-of-order updates, default denial, invalid regions, and concurrency.
- Tests where ancestor and descendant decisions alternate over time.
### Follow-up Questions
1. How would you support moving a region to a different parent?
2. How would you return not only the decision but also the region and timestamp that caused it?
3. Which design would you choose for ten updates per second and one million checks per second?
Quick Answer: Implement timestamp-based access overrides on a static tree of geographic regions. Grants and denials inherit through descendants, later operations win even when they arrive out of order, and each check must find the newest applicable rule on the root-to-region path.
You are given a rooted tree of geographic regions. Implement grant_access(region, timestamp), deny_access(region, timestamp), and check_access(region). A grant or denial at a region applies to that region and its entire subtree. Later operations override earlier operations, including a later operation on a descendant. Explain both a simple implementation and an optimized design for many checks.
Constraints & Assumptions
Timestamps are unique and operations may arrive out of timestamp order.
A region with no applicable operation is denied by default.
The tree is static after construction.
A check must reflect the greatest-timestamp operation among the region and all of its ancestors.
Clarifying Questions to Ask Guidance
Does an older late-arriving operation replace newer state? It should not under timestamp ordering.
What are the expected ratios of updates to checks and the maximum tree depth?
Must access checks be linearizable with concurrent updates?
What a Strong Answer Covers Guidance
A correct inheritance and last-write-wins rule for ancestor and descendant operations.
A baseline parent-walk design with explicit time and space complexity.
An Euler-tour or tree-decomposition optimization suited to the workload.
Handling for out-of-order updates, default denial, invalid regions, and concurrency.
Tests where ancestor and descendant decisions alternate over time.
Follow-up Questions Guidance
How would you support moving a region to a different parent?
How would you return not only the decision but also the region and timestamp that caused it?
Which design would you choose for ten updates per second and one million checks per second?