Fix the backoff bounds in a retry helper

Review the AI's codeEasy10 minAmazon style

Your task

Your service retries a flaky downstream call with exponential backoff and full jitter: each sleep is a random time from 0 up to a bound, and the bound grows after every attempt. A reviewer asks you to defend the schedule, so you want a function that returns just the bounds.

An assistant wrote backoff_delay_bounds(base_ms, factor, max_delay_ms, attempts, context_cancelled). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • base_ms: the first bound, in milliseconds.
  • factor: what each bound is multiplied by to get the next.
  • max_delay_ms: the cap. No bound is ever larger.
  • attempts: how many calls the loop makes. There is one sleep between each pair of calls.
  • context_cancelled: true when the request was cancelled before the loop started.
  • Return the list of bounds, in milliseconds.

Rules the tests check:

  • If context_cancelled is true, return an empty list. Check this first.
  • If attempts is 1 or less, return an empty list.
  • A negative base_ms counts as 0, a factor below 1 counts as 1, and a negative max_delay_ms counts as 0.
  • Return attempts - 1 bounds. The first is base_ms and each next one is the previous bound times factor. Every bound, the first included, is capped at max_delay_ms.

Examples

backoff_delay_bounds(base_ms: int, factor: int, max_delay_ms: int, attempts: int, context_cancelled: bool) -> list[int]
Example 1
Inputbackoff_delay_bounds(100, 2, 2000, 5, False)
Output[100, 200, 400, 800]
Example 2
Inputbackoff_delay_bounds(100, 2, 2000, 5, True)
Output[]
Example 3
Inputbackoff_delay_bounds(100, 2, 300, 4, False)
Output[100, 200, 300]