Admit or Deny Requests Under a Per-Client Rolling Quota
Data Structure DesignAuto-graded · 19 test casesLast updated
Process a time-ordered request log and decide per request whether a client is under its quota in a rolling time window, counting only previously admitted requests.
A server reads a log of incoming requests in order. The i-th request occurs at time arrival_times[i] and comes from the client address client_ids[i]. Each client is allowed at most max_requests admitted requests within any rolling period of length period. For every request, in log order, decide whether it is admitted (1) or denied (0).
Rules
- Handle requests in index order
0, 1, ..., n - 1. Times never decrease along the log, and requests sharing the same time are handled in index order. - For request
iat timet = arrival_times[i]from clientc, count the earlier requestsj < ithat satisfy all of the following:client_ids[j] == c,- request
jwas admitted, arrival_times[j] > t - period.
- A request made exactly at time
t - periodis outside the period and is not counted. - If the count is strictly less than
max_requests, admit requesti(output1). Otherwise deny it (output0). - Denied requests are not remembered: they never contribute to any later count.
- Client addresses are compared as exact strings, with no normalization. Different clients never influence one another.
Return a list of length n whose i-th entry is the decision for request i.
Constraints
1 <= n <= 10^5, wheren = len(arrival_times) == len(client_ids)0 <= arrival_times[i] <= 10^9, andarrival_times[i] <= arrival_times[i + 1]1 <= max_requests <= 10^51 <= period <= 10^9- Each
client_ids[i]is a non-empty string of at most 45 characters. - All values fit in a 32-bit signed integer.
Examples
Example 1
Input: admit_requests([1, 2, 3, 4, 4], ['10.0.0.1', '10.0.0.1', '10.0.0.1', '10.0.0.1', '10.0.0.2'], 2, 3)
Output: [1, 1, 0, 1, 1]
Example 2
Input: admit_requests([5, 5, 5], ['192.168.1.7', '192.168.1.7', '192.168.1.7'], 1, 10)
Output: [1, 0, 0]
Example 3
Input: admit_requests([0, 10, 19, 20], ['1.1.1.1', '1.1.1.1', '1.1.1.1', '1.1.1.1'], 1, 10)
Output: [1, 1, 0, 1]
Hints
Hint 1
Only admitted requests matter for later decisions, so you only need to remember those, grouped by client.
Hint 2
Because times never decrease, the admitted times for one client are already sorted. The ones that have expired are always at the oldest end.
Hint 3
Keep a queue per client in a hash map. Before each check, drop front entries with time <= t - period, then compare the queue length to the quota and append only on admission.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Keep a hash map from each client string to a deque holding the times of that client's admitted requests that might still matter.
For each request at time t from client c:
- Fetch (or create) the deque for
c. - Remove from the front every stored time that is
<= t - period. Those requests fall outside the window(t - period, t]. - If the deque now holds fewer than
max_requestsentries, admit the request: appendtand output1. Otherwise output0and store nothing.
Why it works
- Times are non-decreasing and we only append the current time, so each deque is sorted. Expired entries therefore form a prefix at the front, and popping from the front removes exactly them.
- The cutoff
t - periodnever decreases astgrows, so an entry that has expired for one request is expired for all later ones. Removing it early never changes a future decision. - After the removal step, the deque contains exactly the earlier admitted requests from this client with time
> t - period, which is the quantity the rules ask us to count. - Denied requests are never appended, matching the rule that they do not count. Separate clients use separate deques, so they do not interact.
Complexity
- Time: O(n) amortized. Each admitted time is appended once and removed at most once. Hashing a client string costs at most 45 characters, which is bounded.
- Space: O(n) in the worst case, since each deque holds at most
max_requestsentries and the map has one entry per distinct client.
A naive rescan of all earlier requests per request would be O(n^2), too slow for n = 10^5.
Reference solution
from collections import deque
from typing import List
def admit_requests(arrival_times: List[int], client_ids: List[str], max_requests: int, period: int) -> List[int]:
recent = {}
result = []
for t, c in zip(arrival_times, client_ids):
q = recent.get(c)
if q is None:
q = recent[c] = deque()
cutoff = t - period
while q and q[0] <= cutoff:
q.popleft()
if len(q) < max_requests:
q.append(t)
result.append(1)
else:
result.append(0)
return result