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
C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.
Where you start
public int BackoffFixed(int attempt, int baseMs) {
}
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 C#
public int BackoffFixed(int attempt, int baseMs) {
if (attempt <= 0) return Math.Min(baseMs, 60000);
int value = baseMs;
for (int i = 0; i < attempt; i++) {
value = value * 2;
if (value > 60000) value = 60000;
}
return value;
}