How would you model and test a bounded retry queue in a coding exercise?
Answer
Represent each item with attempt count and next eligible time, process ready items in queue order, and move retryable failures back only while attempts remain. Inject the clock and outcome function so tests are deterministic. Cover success, terminal failure, retry exhaustion, backoff timing, multiple items, duplicate delivery and the invariant that one failing item cannot block all later work.
Strong answer includes
- makes time and outcomes injectable
- defines retry and terminal-failure state transitions
- tests fairness, exhaustion and duplicate delivery
Practical example
A naive implementation re-enqueues a failed item at the front of the queue for an immediate retry, which lets one persistently failing item starve every other item behind it; the fix is to re-enqueue at the back with a computed backoff delay, verified by a test that asserts item two still gets processed while item one is still retrying.