FastPrepBounded HTTP Retry Flow

Bounded HTTP Retry Flow

Benchling logoBenchling● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

For this exercise, assume a client must complete one POST request followed by one GET request over an unreliable connection. Two arrays, postResponses and getResponses, contain the responses that successive attempts would receive for their respective method. Consume each array in order, starting at index zero.

For this exercise, assume a response from 200 through 299 means success. A response of 0 means a transport failure. Retry a transport failure, HTTP 429, or an HTTP response from 500 through 599. Every other response means permanent failure for this exercise.

Each method may make at most maxAttempts attempts, including its initial attempt. Stop that method immediately on success or permanent failure. Running out of recorded responses or attempts before success is failure. Start GET only if POST succeeds, with its own fresh attempt budget. Return true exactly when both methods succeed.

This is a finite response simulation: no network calls, delays, request payloads, or server side effects are modeled. Replaying POST is permitted by this exercise model; the result makes no claim about real POST idempotency.

Function

httpRetryFlow(postResponses: int[], getResponses: int[], maxAttempts: int) → boolean

Examples

Example 1

postResponses = [0,201]getResponses = [503,200]maxAttempts = 2return = true

POST succeeds on its second attempt; GET also succeeds on its second attempt with a fresh budget.

Example 2

postResponses = [400,201]getResponses = [200]maxAttempts = 2return = false

HTTP 400 permanently stops POST. A later recorded success is never consumed, and GET does not start.

Constraints

  • For this exercise, assume 0 <= postResponses.length, getResponses.length <= 1000.
  • Each recorded response is 0 or an integer from 100 through 599.
  • For this exercise, assume 1 <= maxAttempts <= 100.

More Benchling problems

See Benchling hiring insights
public boolean httpRetryFlow(int[] postResponses, int[] getResponses, int maxAttempts) {
    // Write your code here.
}
postResponses[0,201]
getResponses[503,200]
maxAttempts2
expectedtrue
Checking account…