Exponential Backoff with Full Jitter
When a customer’s API throws 503s, everyone retrying on the same doubling schedule stampedes it in sync. Full jitter spreads the retries randomly across the window so the herd disperses. Compute the delay schedule. (Random draws are supplied, so it’s deterministic.) Solve it in Python or TypeScript, with hidden tests.
The problem
Implement backoff_jitter(base, cap, attempts, rand). For attempt i (0-indexed), the exponential ceiling is min(cap, base * 2^i). With full jitter, the actual delay is a uniform random value in [0, ceiling]: here that random fraction is supplied as rand[i] in [0, 1], so the delay is rand[i] * ceiling. Return the list of attempts delays, each rounded to 4 decimals.
base = 1.0, cap = 10.0, attempts = 5, rand = [1.0, 0.5, 1.0, 0.5, 0.0][1.0, 1.0, 4.0, 4.0, 0.0]- Ceiling for attempt
iismin(cap, base * 2^i)— exponential growth, clamped atcap. - Full jitter: delay =
rand[i] * ceiling(a fraction of the ceiling, not the ceiling itself). - Round each delay to 4 decimal places.
randhas at leastattemptsentries.
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_jitter(base, cap, attempts, rand): for each attempt compute the clamped exponential ceiling, multiply by the supplied random fraction, round to 4 decimals, and collect the delays.
- Ceiling:
min(cap, base * (2 ** i))foriin0 … attempts-1. - Full jitter multiplies that ceiling by
rand[i](in [0,1]) — so a retry lands anywhere in the window, not on a synchronized tick. - Round to 4 decimals so floating-point noise doesn’t leak into the result.
Explore the topic
See this challenge alongside everything else on the same subject — handbooks, system designs, algorithms and tools, in one place.