PIXELBANKv9.1.0
Menu

Total Retry Latency with Capped Backoff

Problem Statement

An agent retries a failing call with exponential backoff. Compute the total wall-clock delay spent sleeping between attempts, given a cap on each individual backoff.

Background

With base seconds, factor 2, and attempts total tries, the sleeps happen between attempts: after attempt i (1-indexed) the agent sleeps min(base * 2(i-1), cap)** seconds, for i = 1 .. attempts-1. The final attempt is not followed by a sleep. Return the summed sleep time.

Your Task

Implement:

def total_backoff(base, cap, attempts):

Return the total seconds slept (float) across all inter-attempt waits.

Input Format

  • base (float), cap (float), attempts (int).

Output Format

  • A float (total sleep seconds).

Sample

print(total_backoff(1.0, 8.0, 4))

Output:

7.0

Example:

Input:
print(total_backoff(1.0, 8.0, 4))
Output:
7.0
Reasoning:
  • With attempts = 4, the agent sleeps between attempts 1→2, 2→3, and 3→4, resulting in 3 total sleep intervals.
  • For the first interval (after attempt 1), the backoff is calculated as min⁡(1.0×20,8.0)=min⁡(1.0,8.0)=1.0\min(1.0 \times 2^0, 8.0) = \min(1.0, 8.0) = 1.0 second.
  • For the second interval (after attempt 2), the backoff doubles to min⁡(1.0×21,8.0)=min⁡(2.0,8.0)=2.0\min(1.0 \times 2^1, 8.0) = \min(2.0, 8.0) = 2.0 seconds.
  • For the third interval (after attempt 3), the backoff doubles again to min⁡(1.0×22,8.0)=min⁡(4.0,8.0)=4.0\min(1.0 \times 2^2, 8.0) = \min(4.0, 8.0) = 4.0 seconds.
  • Summing these individual sleep durations gives the total latency: 1.0+2.0+4.0=7.01.0 + 2.0 + 4.0 = 7.0.
  • The final output is 7.0

Constraints:

  • attempts >= 1; with 1 attempt there are no sleeps (return 0.0).
  • Sleep after attempt i is min(base * 2**(i-1), cap) for i in 1..attempts-1.
  • Return a float.
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Total Retry Latency with Capped Backoff - Medium | PixelBank