Weather Station Retries
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
maxRetriesretries. - 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
baseMs = 100 capMs = 1000 maxRetries = 6 budgetMs = 10000
[100, 200, 400, 800, 1000, 1000]
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.
baseMs = 500 capMs = 4000 maxRetries = 10 budgetMs = 6000
[500, 1000, 2000]
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- Hint 1
Write out the first few waits for base 100 and cap 1,000. When does the cap start to matter, and what happens after that?
- Hint 2
Keep the current wait in a variable. After each retry it doubles, but never past the cap:
wait = min(wait × 2, cap). - Hint 3
Keep a running total. If
total + waitwould exceed the budget, stop: every later wait is at least as long, so none can fit. - Hint 4
Loop at most
maxRetriestimes, ending early on the budget check. Double a capped value instead of computing2kfor large k.
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.
How to solve it
Track the next wait and the total so far. Each round, check the budget, record the wait, then double it and clamp to the cap.
Trace the second example, base 500, cap 4,000, budget 6,000:
- wait 500, total 0: 0 + 500 ≤ 6,000. Record 500, total 500, next wait 1,000.
- 500 + 1,000 = 1,500 ≤ 6,000. Record 1,000, total 1,500, next wait 2,000.
- 1,500 + 2,000 = 3,500 ≤ 6,000. Record 2,000, total 3,500, next wait 4,000.
- 3,500 + 4,000 = 7,500 > 6,000. Stop. Answer
[500, 1000, 2000].
Stopping at the first wait that does not fit is safe because waits never shrink. The cap keeps an unlucky client from sleeping for an hour; the budget stops it retrying long after the caller stopped caring.
function retryDelays(baseMs, capMs, maxRetries, budgetMs) {
const waits = [];
let wait = baseMs, total = 0;
for (let k = 1; k <= maxRetries; k++) {
if (total + wait > budgetMs) break; // later waits are never shorter
waits.push(wait);
total += wait;
wait = Math.min(wait * 2, capMs); // double, but never past the cap
}
return waits;
}def retry_delays(base_ms, cap_ms, max_retries, budget_ms):
waits = []
wait, total = base_ms, 0
for _ in range(max_retries):
if total + wait > budget_ms: # later waits are never shorter
break
waits.append(wait)
total += wait
wait = min(wait * 2, cap_ms) # double, but never past the cap
return waitsRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.