← Problems24. Retry with exponential backoffMediumPython
00:00 / 20:00

Retry with exponential backoff

Medium·Acceptance ·Asked at Microsoft, Uber, PayPal

Wrap a flaky call so transient failures are retried and permanent ones are not. Return the result, or raise the last exception once the budget is exhausted.

Input schema

solve(call: Callable[[], Any], attempts: int, base_delay: float, retry_on: tuple[type[Exception], ...], sleep: Callable[[float], None]) -> Any

Example

call fails twice with TimeoutError then returns 7 attempts=5, base_delay=0.5, retry_on=(TimeoutError,) sleep called with 0.5 then 1.0; solve returns 7

Constraints

  • Delay for retry n (1-indexed) is base_delay * 2 ** (n - 1)
  • Exceptions outside retry_on propagate immediately, with no sleep
  • The injected sleep is the only delay mechanism — calling time.sleep fails the timing case
  • attempts counts total calls, not retries

Topics

data modeling

Similar problems

Community-reported interview topic. Not an official company question and no affiliation is implied.

solution.py
Loading editor…
Draft not saved yet · Spaces 4 · UTF-8 · ⌘↵ run, ⌘⇧↵ submit
Nothing run yet

Run against the public tests, or submit to score against all of them.