Problems › TypeScript › network
Exponential backoff with a ceiling
Retries should space out exponentially — double the delay each attempt — but never exceed a sixty-second cap.
- The delay is baseMs multiplied by 2 raised to the power of the attempt number.
- The result is capped at 60000 milliseconds.
- An attempt number of zero or less returns baseMs (still capped).
- Compute 2^attempt by repeated doubling, not a library power function.
backoffFixed(attempt: int, baseMs: int) → int
Where you start
function backoffFixed(attempt: number, baseMs: number): number {
}
Worked examples
| Call | Result |
|---|---|
backoffFixed(0, 100) | 100 |
backoffFixed(1, 100) | 200 |
backoffFixed(2, 100) | 400 |
backoffFixed(3, 100) | 800 |
Hint
Start at baseMs and double in a loop; clamp on each step.
Reference solution in TypeScript
function backoffFixed(attempt: number, baseMs: number): number {
if (attempt <= 0) return Math.min(baseMs, 60000);
let value = baseMs;
for (let i = 0; i < attempt; i++) {
value = value * 2;
if (value > 60000) value = 60000;
}
return value;
}