Drill

ProblemsGo › network

Exponential backoff with a ceiling

easynetworkMathBit manipulationGo

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

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

Go 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.

Solve it in Python →

Where you start

func backoffFixed(attempt int, baseMs int) int {
	
}

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 Go
func backoffFixed(attempt int, baseMs int) int {
	if attempt <= 0 {
		if baseMs > 60000 {
			return 60000
		}
		return baseMs
	}
	value := baseMs
	for 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 Go