Fix the request pacing for a rate-limited API
Your task
An AI synced issues from an API that allows 60 requests a minute with Promise.all(ids.map(fetch)). It fired every request at once, and the integration's token was banned for an hour. The new client sends requests in order on a schedule that respects two limits: how many may start in the same second, and how many may start in any 60 seconds.
An assistant wrote plan_request_schedule(request_count, requests_per_minute, max_burst), which computes that schedule. It is in the editor. It has a bug. Find it and fix it.
Input and output:
request_count: how many requests to send.requests_per_minute: the most requests that may start in any 60 second window.max_burst: the most requests that may start in the same second.- Return the second at which each request starts, in order.
Rules the tests check:
- If any argument is 0 or less, return
[]. - Requests start in order, each at a whole second. Request i, counting from 0, starts at the earliest second that meets all of these:
- it is 0 or later
- when i is at least
max_burst, it is at least 1 second after the start of the requestmax_burstplaces earlier - when i is at least
requests_per_minute, it is at least 60 seconds after the start of the requestrequests_per_minuteplaces earlier
Examples
plan_request_schedule(request_count: int, requests_per_minute: int, max_burst: int) -> list[int]Example 1
Input
plan_request_schedule(5, 60, 2)Output
[0, 0, 1, 1, 2]Example 2
Input
plan_request_schedule(4, 3, 2)Output
[0, 0, 1, 60]Example 3
Input
plan_request_schedule(3, 60, 60)Output
[0, 0, 0]