← 返回 atlassian 的题目列表Implement Rate Limiter Function (Time Bucket Based)
类型:online_judge
Problem: Implement a Time-Bucket-Based Rate Limiter
Implement a rate limiter with the function:
boolean shouldPass(int timeBucket)
timeBucket is the bucket id of the incoming request (e.g., epoch second if bucketing by second).
Rate limiting rule: within any rolling window of X consecutive buckets, allow at most Y requests.
For each request:
return true if it is allowed;
return false if it is rejected.
Important contract
Every request attempt must be counted, regardless of whether it was allowed or rejected.
timeBucket may jump forward (e.g., from 100 to 150).
I/O for this coding task
Read from stdin:
Line 1: two integers X Y (window size, max requests per window)
Line 2: integer N (number of requests)
Next N lines: each contains an integer timeBucket
Print true/false per request, one per line.
Constraints
1 <= X <= 1e6
1 <= Y <= 1e6
1 <= N <= 2e5
timeBucket is a 32-bit non-negative integer
Example
Input:
3 2
6
1
1
2
3
3
4
Output:
true
true
false
true
false
true
Example
Input
3 2
6
1
1
2
3
3
4
Output
true
true
false
true
false
true