Skip to main content

Exponential Backoff Schedule

EasyReliabilityRetriesMathBackend Systems

Description

A client retries a failed request with exponential backoff. The first retry waits baseMs milliseconds, each following retry waits multiplier times the previous uncapped wait, and no wait may exceed capMs. Given baseMs, multiplier, capMs, and maxRetries, return the wait before each retry, in order, as an array of maxRetries integers. Once a wait reaches the cap, every remaining wait is capMs.

Examples

Input:baseMs = 100, multiplier = 2, capMs = 1000, maxRetries = 5
Output:[100,200,400,800,1000]
Explanation:

The delays double from 100 milliseconds: 100, 200, 400 and 800. The fifth uncapped delay would be 1600, which exceeds the cap, so the client sleeps for the cap of 1000 instead.

Input:baseMs = 250, multiplier = 3, capMs = 2000, maxRetries = 4
Output:[250,750,2000,2000]
Explanation:

The delays triple: 250, then 750. The third uncapped delay would be 2250, above the cap, so it is held at 2000, and once the cap is reached every later delay stays at the cap.

Input:baseMs = 500, multiplier = 1, capMs = 400, maxRetries = 3
Output:[400,400,400]
Explanation:

The base delay already exceeds the cap, so every retry sleeps for the capped 400 milliseconds. A multiplier of 1 would not grow the delay in any case.

Constraints

  • •1 ≤ baseMs ≤ 10⁶
  • •1 ≤ multiplier ≤ 10
  • •1 ≤ capMs ≤ 10⁶
  • •0 ≤ maxRetries ≤ 50

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.