Reliability algorithms

Retry delays, with a bug

Prepare a deterministic exponential-backoff schedule. This function computes delays; it does not sleep or send requests.

In production

Retry delays shape client storms. An uncapped or off-by-one sequence either hammers a dependency or gives up too soon.

What it must return

Return the delay before each retry attempt, doubling from base_ms and never exceeding cap_ms. Zero attempts means an empty list.

  • attempts, base_ms and cap_ms are nonnegative integers. Attempt zero waits min(base_ms, cap_ms); each later delay doubles until capped. Return one delay per retry.
  • Do not mutate input values. Use the Python standard library only.

Edge cases

  • Carry the capped delay forward so the next doubling starts from the cap.
  • attempts == 0 yields [].
  • The list length equals attempts.

Signature

retry_delays(attempts, base_ms, cap_ms)

This version runs but gets some cases wrong. Run the examples first to see which case fails.

Hints

  1. Second hint

  2. A partial sketch

The reference solution opens after the last hint.

Adapted from Baodium Interview Coach.

solution.py · Python

Loading editor…

Cap reached

attempts

5

base_ms

100

cap_ms

500

Expected

[
  100,
  200,
  400,
  500,
  500
]

Each run uses a fresh Python runtime in this browser, with a 3-second limit and a Stop button. The first run downloads the runtime. 0 of 10 in Reliability algorithms passed in Python on this device.

Next: Failure streak opening, partly written