Workspace/Coding labs
Loading progress

Build a deadline-aware retry simulator

Advanced60 min

Implement simulate_retry(outcomes, deadline, max_attempts=3, base=1.0, idempotent=True). Each outcome is a (duration, status) pair. Return (terminal_status, tuple_of_start_times, elapsed_time) without real waiting.

Your task

  1. Accept nonnegative finite deadline, base, and durations; require positive integer max_attempts excluding booleans, boolean idempotent, and status ok, transient, or permanent. Raise ValueError for invalid inputs.
  2. An attempt starts only strictly before the deadline. A result arriving exactly at the deadline can succeed. A duration exceeding remaining time ends at the deadline with status deadline.
  3. Stop on ok or permanent. Stop on the first transient outcome with status unsafe if idempotent is false.
  4. Between retryable attempts wait base * 2 ** (attempt_number - 1). If the next start would be at or after the deadline, return deadline with elapsed_time equal to the deadline.
  5. When the attempt limit or supplied outcomes are exhausted after a transient result, return exhausted without an extra wait. No outcomes returns exhausted at time zero unless the deadline is zero, which returns deadline.
  6. Do not sleep or call a network. This is a deterministic policy simulator; real jitter and receiver deduplication are outside its contract.

Examples

EXAMPLE 1

Inputsimulate_retry([(1, "transient"), (1, "ok")], 5)

Output("ok", (0.0, 2.0), 3.0)

The second attempt follows one second of work and one second of backoff.
solution.pyPython 3.12