Retry with Exponential Backoff

Hard ⏱ 18 min 24% acceptance ★★★★☆ 4.2
Write get_with_retry(url, attempts=3) that retries failed GETs (network errors or 5xx statuses), sleeping 2 ** attempt seconds between tries (time.sleep), and re-raising after the last failure. 4xx errors must NOT be retried — the client is wrong, retrying won't help.

Examples

Example 1
Input
get_with_retry(flaky_url)
Output
the JSON once a try succeeds; the error after 3 failures
Explanation

Backoff: 1 s, then 2 s, then give up.

Constraints

  • Retry only network errors and 5xx.
  • Exponential sleep between attempts.

Topics

APIsretries

Companies

AmazonGoogleCloudflare

Hints

Hint 1

response.status_code >= 500 is retryable.

Hint 2

Track the last exception for the final re-raise.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.