Bounded HTTP Retry Flow
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) → booleanExamples
Example 1
postResponses = [0,201]getResponses = [503,200]maxAttempts = 2return = truePOST 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 = falseHTTP 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
0or an integer from100through599. - For this exercise, assume
1 <= maxAttempts <= 100.