AlgoMaster Logo
AlgoMasterEnforce Hierarchical API Quotashard

Enforce Hierarchical API Quotas

hard

API platforms often enforce both a tenant-wide budget and a smaller per-user budget. A request is safe only when it fits both levels, and rejecting it must not partially consume either budget.

Design a HierarchicalQuotaLimiter class:

  • HierarchicalQuotaLimiter(int windowSize, int tenantLimit, int userLimit) configures fixed-window cost budgets.
  • String allow(String tenantId, String userId, int cost, int timestamp) evaluates one request.

The window ID is integer division timestamp / windowSize. Timestamps are nondecreasing across calls. Within each window:

  • Every tenant may consume at most tenantLimit total units across all users.
  • Every (tenantId, userId) pair may consume at most userLimit units.
  • Equal user IDs in different tenants are unrelated.

Check the projected tenant total first. Return "TENANT_LIMIT" if it would exceed the tenant limit. Otherwise check the projected user total and return "USER_LIMIT" if it would exceed the user limit. Rejections consume no units.

On success, charge both counters and return "ALLOWED:T:U", where T and U are the remaining tenant and user units after the charge.

Example 1:

Input:

Output:

Explanation: The failed user charge is atomic, leaving six tenant units for u2.

Example 2:

Input:

Output:

Explanation: The boundary starts a new window, and the two tenant-scoped users are independent.

Constraints

  • 1 <= windowSize, tenantLimit, userLimit <= 10^9
  • 1 <= tenantId.length, userId.length <= 100; IDs contain only letters, digits, _, and -.
  • 1 <= cost <= 10^9
  • 0 <= timestamp <= 10^9
  • Timestamps are nondecreasing.
  • At most 10^4 calls are made to allow.
Hints

Loading...
CallReturns
new HierarchicalQuotaLimiter(10, 10, 6)null
allow("acme", "u1", 4, 0)"ALLOWED:6:2"
allow("acme", "u1", 3, 1)"USER_LIMIT"
allow("acme", "u2", 6, 2)"ALLOWED:0:0"
allow("acme", "u3", 1, 3)"TENANT_LIMIT"

The rejected u1 request consumes nothing, so u2 can use all six remaining tenant units. The tenant is then exhausted.

Run checks these cases. Submit also runs a larger hidden set.