Simulate a Circuit Breaker's Closed, Open, and Half-Open States
Last updated: November 29, 2026
Quick Overview
Implement a client-side circuit breaker for calls to a backend: in CLOSED it counts consecutive failures and trips to OPEN after a threshold; in OPEN it rejects calls until a cooldown passes, then moves to HALF_OPEN, where one trial call decides between CLOSED and OPEN. Given a timeline of call attempts and their backend outcomes, return what the breaker does with each.
0
0
0 solved
Implement a client-side circuit breaker for calls to a backend: in CLOSED it counts consecutive failures and trips to OPEN after a threshold; in OPEN it rejects calls until a cooldown passes, then moves to HALF_OPEN, where one trial call decides between CLOSED and OPEN. Given a timeline of call attempts and their backend outcomes, return what the breaker does with each.
A Google onsite question that ties a reliability pattern from large RPC systems to a precise state machine. The interviewer checks correct transitions, time-based state changes evaluated lazily, and that rejected calls never reach the backend.
What the Interviewer Expects
- Draw the three states and every transition before coding
- Evaluate the OPEN to HALF_OPEN timeout lazily on the next call
- Reset the failure counter on success in CLOSED
- Allow only one trial call in HALF_OPEN
- Inject time through parameters so the breaker is testable
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you use an error rate over a sliding window instead of consecutive failures?
- How do you allow several concurrent trial calls safely in HALF_OPEN?
- How would breakers on thousands of clients avoid synchronized retries against a recovering backend?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: threshold failures, cooldown seconds, and a list of (t, ok) attempts in time order, where ok is what the backend would return if called. Output per attempt: CALL_OK, CALL_FAIL, or `...
High-Level Design
A class with state, failures, opened_at. The attempt(t, ok) method first applies time-based transitions (OPEN to HALF_OPEN if the cooldown elapsed), then dispatches on state. Keeping the backe...