FastPrepConfigurable Retry Strategies

Configurable Retry Strategies

Airbnb logoAirbnb● MediumFULLTIMEOA

Updated

Problem statement

You are simulating a client that retries a request. The list outcomes gives the result of each attempt, in order. Each outcome is either FAILURE or SUCCESS:value.

The client uses one retry strategy:

  • ONCE allows at most one retry after the first attempt, even when maxRetries is larger. It waits baseDelay milliseconds before that retry. If maxRetries is zero, ONCE makes no retry.
  • FIXED allows at most maxRetries retries. It waits baseDelay milliseconds before each retry.
  • EXPONENTIAL allows at most maxRetries retries. The retries are numbered from k = 1. Before retry number k, it waits baseDelay * 2^(k - 1) milliseconds.

Go through outcomes from left to right. The first attempt starts at elapsed time 0. Stop at the first success, or when the strategy allows no more retries. The input always has enough outcomes for every attempt the strategy allows.

If an attempt succeeds, return SUCCESS:value|attempts|elapsed. If every allowed attempt fails, return FAILED|attempts|elapsed. Here attempts counts every attempt made, including the first one. elapsed is the total of the waits before the retries that actually ran.

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 first attempt fails. ONCE allows one retry, which runs after a 10-millisecond wait. That retry succeeds, so the answer reports two attempts and that one wait as the elapsed time.

Example 2

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

The first two attempts fail. The two retries that run wait 5 and then 10 milliseconds. The third attempt succeeds, after 15 milliseconds in total.

Example 3

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

The first attempt fails. ONCE allows only one retry, and that retry fails too. The client stops there and returns FAILED, so the later success is never checked.

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 Online Assessment questions

Similar questions at other companies

Airbnb interview experiences

All Airbnb interview questionsSee Airbnb hiring insights
Saved
class Solution {
    public String simulateRetries(String[] outcomes, String strategy, int maxRetries, long baseDelay) {
        // Write your code here.
    }
}
Checking account…