Drill

ProblemsPython › network

Exponential backoff with a ceiling

easynetworkPython

Retries should space out exponentially — double the delay each attempt — but never exceed a sixty-second cap.

backoff_fixed(attempt: int, base_ms: int) → int

Solve it in the editor →

Where you start

def backoff_fixed(attempt: int, base_ms: int) -> int:
    

Worked examples

CallResult
backoff_fixed(0, 100)100
backoff_fixed(1, 100)200
backoff_fixed(2, 100)400
backoff_fixed(3, 100)800

Hint

Start at baseMs and double in a loop; clamp on each step.

Reference solution in Python
def backoff_fixed(attempt: int, base_ms: int) -> int:
    if attempt <= 0:
        return min(base_ms, 60000)
    value = base_ms
    for _ in range(attempt):
        value = value * 2
        if value > 60000:
            value = 60000
    return value

The same problem in another language

More network problems in Python