PIXELBANKv9.1.0
Menu

Retry with Capped Exponential Backoff and Jitter Cap

Problem Statement

Generate the backoff delay schedule for a retrying pipeline step: exponential growth, capped at a maximum, for a given number of retries.

Background

For retry attempt i (0-indexed), the base delay is base * (2 ** i), capped at cap. Return the list of delays for attempts 0 .. retries-1. This is the deterministic (pre-jitter) schedule used to reason about worst-case total wait.

Your Task

def backoff_schedule(base, cap, retries):

Return the list of capped delays.

Input Format

  • base (number), cap (number), retries (int).

Output Format

  • A list of numbers.

Sample

print(backoff_schedule(1, 10, 5))

Output:

[1, 2, 4, 8, 10]

Example:

Input:
print(backoff_schedule(1, 10, 5))
Output:
[1, 2, 4, 8, 10]
Reasoning:
  • Initialize the parameters from the input: base delay b=1b = 1, maximum cap c=10c = 10, and total retries n=5n = 5.
  • For the first two attempts (i=0,1i=0, 1), calculate the exponential delay b⋅2ib \cdot 2^i and compare it to the cap. Since 1⋅20=11 \cdot 2^0 = 1 and 1⋅21=21 \cdot 2^1 = 2 are both less than 1010, the delays remain 11 and 22.
  • For the next two attempts (i=2,3i=2, 3), the exponential growth continues: 1⋅22=41 \cdot 2^2 = 4 and 1⋅23=81 \cdot 2^3 = 8. Both values are still below the cap of 1010, so the delays are 44 and 88.
  • For the final attempt (i=4i=4), the calculated exponential delay is 1⋅24=161 \cdot 2^4 = 16. Because this exceeds the cap, the delay is constrained to the maximum value of 1010.
  • The final output is [1, 2, 4, 8, 10]

Constraints:

  • delay[i] = min(base * 2**i, cap).
  • Return retries entries (empty if retries == 0).
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.