Wrap a flaky tool with retries, exponential backoff, and a max wall-clock budget.
Full problem description visible. Upgrade to unlock the editor, test cases, and solution.
Implement call_with_retry(fn, *args, max_attempts=5, base=0.5, max_seconds=10).
- Retry on any exception.
- Sleep base * 2**attempt seconds between attempts (with small jitter).
- Stop if cumulative wait exceeds max_seconds or attempts exhausted.
- Re-raise the last exception on failure.
₹999/monthLimited period launch pricing.
Get the full problem statement, test cases, interactive editor, solution explanation, and visual diagram.
import time, random
def call_with_retry(fn, *args, max_attempts=5, base=0.5, max_seconds=10, **kwargs):
elapsed = 0.0
last_exc = None
for attempt in range(max_attempts):
try:
return fn(*args, **kwargs)
except Exception as e:
last_exc = eBackoff + budget check
raise last_exc
Already have Pro access? Sign in