InferenceAI

Enter at least two characters to search.

Start free

Token Bucket Admission

hardProduction AI35 min50 XP

Requests are sorted [timestamp_seconds, token_cost] pairs. Implement token_bucket_admission(requests, capacity, refill_per_second) and return one boolean per request. Begin with a full bucket at the first timestamp. Before each request, refill by elapsed seconds times refill_per_second, capped at capacity. Allow and deduct cost only if enough tokens remain; rejected requests consume none. Numeric inputs are nonnegative and timestamps are nondecreasing. Floating-point comparisons may be used directly.

Examples

EXAMPLE WALKTHROUGHburst and refill

A guided view of the published example, not a trace of your code.

INPUT[[[0,3],[0,3],[2,2]],5,1]
TASKtoken_bucket_admission()
EXPECTED OUTPUT[true,false,true]

Start with the input values shown in this example.

Example 1 of 2

Input: requests = [[0,3],[0,3],[2,2]], capacity = 5, refill_per_second = 1

Output: [true,false,true]

Explanation: burst and refill

Input: requests = [[10,2],[11,2]], capacity = 2, refill_per_second = 2

Output: [true,true]

Explanation: full refill

Hint 1

The bucket starts full at the first event time.

Hint 2

A denied request leaves the balance unchanged.

Topics

PythonRate LimitingInference APIs