Retry with Exponential Backoff
Tools time out and APIs rate-limit — a production agent has to retry without hammering. Exponential backoff waits longer after each failure (1s, 2s, 4s, 8s…) up to a cap, so the system backs off instead of piling on. Compute the delay schedule.
The problem
Implement backoff_delays(retries, base, cap). Return the list of wait times before each retry attempt, where the delay for attempt i (0-indexed) is base × 2ⁱ, capped at cap. There are retries attempts in total, so the list has retries entries.
retries=5, base=1, cap=10[1, 2, 4, 8, 10]retries=3, base=2, cap=100[2, 4, 8]retries=0, base=1, cap=10[]- Delay for attempt i =
min(cap, base × 2ⁱ), with i from 0 to retries − 1. - The result always has exactly
retrieselements. - Pure arithmetic — no sleeping, no randomness (that would be jitter, a separate concern).
Your turn — write it
Edit the stub, hit Run (or ⌘/Ctrl + Enter), and watch the hidden tests. Stuck? the hints are right above and Reveal solution is one click away.
Implement backoff_delays(retries, base, cap) — the exponential-backoff wait schedule, each delay doubling from base and clamped at cap.
- Loop i from 0 to retries − 1.
- The raw delay is base shifted left i times: base × 2**i.
- Clamp each delay with min(cap, …) so it never exceeds the cap.
Explore the topic
See this challenge alongside everything else on the same subject — handbooks, system designs, algorithms and tools, in one place.