FastPrepConfigurable Retry Strategies

Configurable Retry Strategies

Airbnb logoAirbnb● MediumFULLTIMEOA
Learn

Problem statement

Simulate a retry client whose ordered attempt outcomes are provided in outcomes. Each outcome is either FAILURE or SUCCESS:value.

The client selects one retry strategy:

  • ONCE permits at most one retry after the initial attempt.
  • FIXED permits at most maxRetries retries and waits baseDelay milliseconds before each retry.
  • EXPONENTIAL permits at most maxRetries retries. The wait before retry number k, where the first retry is k = 1, is baseDelay * 2^(k - 1) milliseconds.

Inspect outcomes from left to right, beginning with the initial attempt at elapsed time 0. Stop at the first success or when the selected retry budget is exhausted. The input always contains enough outcomes for every permitted attempt.

Return SUCCESS:value|attempts|elapsed after a success, or FAILED|attempts|elapsed when every permitted attempt fails. attempts includes the initial attempt, and elapsed is the sum of waits before executed retries.

Function

simulateRetries(outcomes: String[], strategy: String, maxRetries: int, baseDelay: long) → String

Examples

Example 1

outcomes = ["FAILURE","SUCCESS:ok"]strategy = "ONCE"maxRetries = 5baseDelay = 10return = "SUCCESS:ok|2|10"

The initial attempt fails. ONCE permits one retry after a 10-millisecond wait, and that retry succeeds.

Example 2

outcomes = ["FAILURE","FAILURE","SUCCESS:ready","FAILURE"]strategy = "EXPONENTIAL"maxRetries = 3baseDelay = 5return = "SUCCESS:ready|3|15"

The two executed retries wait 5 and 10 milliseconds. The third attempt succeeds after 15 total milliseconds.

Example 3

outcomes = ["FAILURE","FAILURE","SUCCESS:late","FAILURE"]strategy = "ONCE"maxRetries = 3baseDelay = 7return = "FAILED|2|7"

ONCE stops after the initial attempt and one retry, so the later success is never inspected.

Constraints

  • strategy is ONCE, FIXED, or EXPONENTIAL.
  • 0 <= maxRetries <= 30.
  • 1 <= baseDelay <= 10^6.
  • outcomes.length is at least the maximum number of attempts allowed by the selected strategy.
  • Every outcome is exactly FAILURE or begins with SUCCESS:.
  • Every elapsed-time result fits in a signed 64-bit integer.

More Airbnb problems

See Airbnb hiring insights
public String simulateRetries(String[] outcomes, String strategy, int maxRetries, long baseDelay) {
    // Write your code here.
}
outcomes["FAILURE","SUCCESS:ok"]
strategy"ONCE"
maxRetries5
baseDelay10
expected"SUCCESS:ok|2|10"
Checking account…