Drill

ProblemsTypeScript › network

Exponential backoff with a ceiling

easynetworkTypeScript

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

backoffFixed(attempt: int, baseMs: int) → int

Solve it in the editor →

Where you start

function backoffFixed(attempt: number, baseMs: number): number {
  
}

Worked examples

CallResult
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;
}

The same problem in another language

More network problems in TypeScript