Weather Station Retries

EasysimulationAlso known as Exponential Backoff with a Cap

Two hundred hillside weather stations upload a reading every few minutes. When the upload server restarts, every station fails at once, and retrying every 100 ms would knock it over again. So each station waits longer after every failure: 100 ms, 200, 400, doubling up to a ceiling. Old readings are useless, so a station gives up when its total waiting would pass a time budget.

Every network client needs this schedule (see Idempotency and Retries and Reliability and Failure). Real clients add random jitter, such as a random wait between 0 and the scheduled value, so they do not retry in lockstep. You compute the schedule the jitter draws from.

Your task

Write retryDelays(baseMs, capMs, maxRetries, budgetMs). Return the list of waits, in milliseconds, before retry 1, retry 2, and so on.

  • The wait before retry k (counting from 1) is min(capMs, baseMs × 2k−1).
  • There are at most maxRetries retries.
  • Stop before any retry whose wait would make the total of all waits so far greater than budgetMs. A total exactly equal to the budget is allowed.

Return an empty list if no retry fits.

retryDelays(baseMs: int, capMs: int, maxRetries: int, budgetMs: int) → int[]

Examples

Example 1
Input
baseMs = 100
capMs = 1000
maxRetries = 6
budgetMs = 10000
Output
[100, 200, 400, 800, 1000, 1000]
Why

The waits double from 100: 100, 200, 400, 800. The next would be 1,600, but the cap is 1,000, so retries 5 and 6 wait 1,000 each. The total is 3,500 ms, well inside the 10,000 ms budget, so all 6 retries happen.

Example 2
Input
baseMs = 500
capMs = 4000
maxRetries = 10
budgetMs = 6000
Output
[500, 1000, 2000]
Why

After three retries the station has waited 500 + 1,000 + 2,000 = 3,500 ms. Retry 4 would wait 4,000 ms more, for a total of 7,500, which is over the 6,000 ms budget, so the station gives up there. It does not look for a later, shorter wait: waits never shrink.

Constraints

  • 1 ≤ baseMs ≤ capMs ≤ 3,600,000
  • 0 ≤ maxRetries ≤ 60
  • 0 ≤ budgetMs ≤ 1,000,000,000
Start with brute force

A first version computes baseMs × 2k−1 with a power function, then applies the cap. JavaScript and Python get it right, but the middle value is huge: for retry 60 with a 1,000 ms base, about 5.8 × 1020 ms. With 64-bit integers (limit about 9.2 × 1018) that overflows and can go negative, and a negative wait means retrying instantly in a tight loop, which is exactly the retry storm that backoff exists to prevent.

Doubling an already capped value stays within the constraints, avoiding the problem for free.

Hints

0 of 4 shown

Walkthrough and solution

Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.

Ctrl+Enter runs

Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.

Write code