Back off between retries

Back off between retries

Scale AI LLM Interview Question

A model API returns 429 when you send requests too quickly, and retrying immediately makes it worse. The standard answer is to wait longer after each failure, up to a ceiling.

Write a function backoff(attempts, base, cap) that returns the delay before each attempt. The first attempt waits 0 seconds, and each attempt after it waits twice as long as the one before, starting at the base delay, never exceeding the cap. Return a list with one delay per attempt.

Asked of

  • AI Engineer
  • ML Engineer
  • Data Scientist

Example 1

Input

attempts = 6, base = 2, cap = 10

Output

[0, 2, 4, 8, 10, 10]

Example 2

Input

attempts = 3, base = 1, cap = 100

Output

[0, 1, 2]

Explanation

With a base of 2 and a cap of 10 the delays double from 2 to 4 to 8, and the next would be 16, so it is held at the cap of 10. The first attempt is not a retry and waits for nothing.

Submit also runs 3 hidden test cases that check edge cases.

Similar questions