Circuit Breaker State Machine
Updated
Problem statement
A circuit breaker protects a service by blocking requests after too many failures. Simulate one over a list of requests. It starts in the CLOSED state. Each row of requests is [timestamp, outcome], where outcome is SUCCESS or FAILURE. Timestamps never go down, and rows with the same timestamp are handled in list order.
CLOSED: every request is allowed. A success resets the count of failures in a row to 0. A failure adds 1. When the count reachesfailureThreshold, the breaker opens untiltimestamp + openDuration.OPEN: a request before the reopen time is rejected, and its outcome is ignored.- The first request at or after the reopen time is a trial request, sent in the
HALF_OPENstate. If it succeeds, the breaker closes and the failure count goes back to 0. If it fails, the breaker opens again until that request'stimestamp + openDuration.
Return one string per row: REJECTED:OPEN if the request was rejected, or ALLOWED: followed by the state right after the request, such as ALLOWED:CLOSED.
Function
runCircuitBreaker(requests: String[][], failureThreshold: int, openDuration: int) → String[]Examples
Example 1
requests = [["0","SUCCESS"],["1","FAILURE"],["2","FAILURE"],["3","SUCCESS"],["7","SUCCESS"]]failureThreshold = 2openDuration = 4return = ["ALLOWED:CLOSED","ALLOWED:CLOSED","ALLOWED:OPEN","REJECTED:OPEN","ALLOWED:CLOSED"]Time 0 succeeds while closed. The failures at times 1 and 2 reach the threshold of 2, so the breaker opens until 2 + 4 = 6. The request at time 3 comes before 6, so it is rejected. The request at time 7 is the trial request, and it succeeds, so the breaker closes.
Example 2
requests = [["0","FAILURE"],["1","FAILURE"],["4","FAILURE"],["5","SUCCESS"],["7","SUCCESS"]]failureThreshold = 2openDuration = 3return = ["ALLOWED:CLOSED","ALLOWED:OPEN","ALLOWED:OPEN","REJECTED:OPEN","ALLOWED:CLOSED"]The failures at times 0 and 1 open the breaker until 1 + 3 = 4. The request at time 4 is the trial request. It fails, so the breaker opens again until 4 + 3 = 7. Time 5 is rejected. The trial request at time 7 succeeds and closes the breaker.
Constraints
1 ≤ requests.length ≤ 200000- Each row has two strings: a timestamp (a non-negative whole number) and either
SUCCESSorFAILURE. - Timestamps never go down and are at most
10^18. 1 ≤ failureThreshold ≤ 10^91 ≤ openDuration ≤ 2000000000
More Wise interview questions
Similar questions at other companies
- Random Dasher Set OperationsDoorDash · ONSITE INTERVIEWDoorDash interview questions
- Path Leaf Character IteratorGoogle · ONSITE INTERVIEWGoogle interview questions
- Escape Room LeaderboardPinterest · PHONE SCREEN · ONSITE INTERVIEWPinterest interview questions
- Thread-Safe Task Storage and Worker DispatchNuro · ONSITE INTERVIEWNuro interview questions
- Implement MallocNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Removal PoolCitadel · ONSITE INTERVIEWCitadel interview questions
- Shared Counter Snapshot SumTesla · ONSITE INTERVIEWTesla interview questions
- Concurrent Buffered File LoggerDatabricks · ONSITE INTERVIEWDatabricks interview questions
- Memory AllocatorMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Ring Buffer OperationsAmazon · ONSITE INTERVIEW