HardPro challengePythonJavaScriptTypeScript

Circuit Breaker

Node.jsResilienceState Machine

States: CLOSED → OPEN (after threshold failures) → HALF_OPEN (after
cooldown ms) → CLOSED (on success) or OPEN (on failure).

solve(threshold, cooldown, calls) where each call is [time, 'success' | 'fail'].
Returns array of 'allowed' or 'rejected' (rejected = call short-circuited).

Sample tests

Test #1HALF_OPEN fail re-opens
Input: [2,50,[[0,"fail"],[1,"fail"],[60,"fail"],[61,"success"]]]
Output: ["allowed","allowed","allowed","rejected"]
Test #2Threshold 1 trips immediately
Input: [1,10,[[0,"fail"],[5,"success"]]]
Output: ["allowed","rejected"]
Test #3No failures stays closed
Input: [3,100,[[0,"success"],[1,"success"]]]
Output: ["allowed","allowed"]
Test #4Trips after 3 failures
Input: [3,100,[[0,"fail"],[1,"fail"],[2,"fail"],[3,"success"]]]
Output: ["allowed","allowed","allowed","rejected"]
Test #5Recovers after cooldown
Input: [2,50,[[0,"fail"],[1,"fail"],[60,"success"],[61,"success"]]]
Output: ["allowed","allowed","allowed","allowed"]