00:00

Problem Statement

Input/Output Examples

Visual Explanation

The programs record the delays instead of sleeping, through a small Sleeper interface. That makes the output exact and the tests fast.

Approaches

Both approaches produce the same delays. The first is a loop; the second is recursive.

Java Solutions

Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.

Complexity Analysis

The work is one call per attempt. The total waiting time is base × (2a-1 - 1) for a attempts, so it grows quickly.

Interview Tips and Recommended Approach

  • Add jitter in production. A random part of the delay stops many clients from retrying at the same moment.
  • Cap the delay so a long outage does not produce waits of minutes.
  • Retry only safe failures. Timeouts and 503s are good candidates; a validation error will fail again.
  • Make the call idempotent, or a retried payment may be charged twice.

Recommended Approach: Retry Loop

Why: a loop is easy to read and test, and it is where jitter, a delay cap and per-exception rules are simplest to add.