Reliability algorithms

A token bucket

Decide whether each timestamped request is admitted by a per-process token bucket.

In production

Admission control protects a process from burst overload. Admitting a rejected request, or refilling on a reject, breaks the limit.

What it must return

Start full at time 0. Before each request, refill by elapsed time up to capacity; admitting spends one token; rejects spend none.

  • Timestamps are nonnegative seconds in nondecreasing order. capacity is a positive integer; refill_per_second is nonnegative. Start full at time 0. Refill to capacity before each request; admitting consumes one token. Return booleans.
  • Do not mutate input values. Use the Python standard library only.

Edge cases

  • Rejected requests do not consume tokens.
  • Simultaneous requests do not get a magical extra refill between them.
  • Timestamps are nondecreasing.

Signature

admit_requests(timestamps, capacity, refill_per_second)

Hints

  1. Second hint

  2. A partial sketch

The reference solution opens after the last hint.

solution.py · Python

Loading editor…

Burst and refill

timestamps

[
  0,
  0,
  0,
  1,
  1
]

capacity

2

refill_per_second

1

Expected

[
  true,
  true,
  false,
  true,
  false
]

Each run uses a fresh Python runtime in this browser, with a 3-second limit and a Stop button. The first run downloads the runtime. 0 of 10 in Reliability algorithms passed in Python on this device.

Next: When the circuit opens