CODING CHALLENGE · N°14

Retry with Exponential Backoff

Easy AI EngineeringAgentsReliability

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.

EXAMPLE 1
Input retries=5, base=1, cap=10
Output [1, 2, 4, 8, 10]
doubles each time, then clamps at the cap
EXAMPLE 2
Input retries=3, base=2, cap=100
Output [2, 4, 8]
never reaches the cap
EXAMPLE 3
Input retries=0, base=1, cap=10
Output []
no retries → no delays
CONSTRAINTS
  • Delay for attempt i = min(cap, base × 2ⁱ), with i from 0 to retries − 1.
  • The result always has exactly retries elements.
  • Pure arithmetic — no sleeping, no randomness (that would be jitter, a separate concern).
SOLVE IT YOURSELF

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.

YOUR TASK

Implement backoff_delays(retries, base, cap) — the exponential-backoff wait schedule, each delay doubling from base and clamped at cap.

HINTS — 3 IDEAS
  1. Loop i from 0 to retries − 1.
  2. The raw delay is base shifted left i times: base × 2**i.
  3. Clamp each delay with min(cap, …) so it never exceeds the cap.
CPython · WebAssembly

Explore the topic

See this challenge alongside everything else on the same subject — handbooks, system designs, algorithms and tools, in one place.