PIXELBANKv9.1.0
Menu

Circuit Breaker Transitions Over an Event Stream

Problem Statement

Simulate a circuit breaker protecting a flaky tool. Process a stream of call outcomes and report the breaker's state after each.

Background

The breaker has three states. It starts closed (calls allowed). It trips to open after fail_threshold consecutive failures. While open, calls are blocked; the outcomes in the stream while open are skipped (the breaker does not see them) until a probe. After cooldown events have elapsed since opening, it moves to half_open and allows the next outcome as a probe: a success closes it (reset failure count), a failure re-opens it (reset the cooldown clock).

Simplified model for this exercise: iterate outcomes ("ok"/"fail"). Maintain state, a consecutive-failure counter, and an opened_at index.

  • closed: on "fail" increment counter; if it reaches fail_threshold, go open and record opened_at. On "ok" reset counter.
  • open: if current_index - opened_at >= cooldown, switch to half_open and process this outcome as a probe; otherwise stay open.
  • half_open: "ok" -> closed (counter 0); "fail" -> open (record opened_at = current index, counter = fail_threshold).

Record the state string after processing each outcome.

Your Task

def circuit_states(outcomes, fail_threshold, cooldown):

Return the list of state strings, one per outcome.

Input Format

  • outcomes (list of "ok"/"fail"), fail_threshold (int), cooldown (int).

Output Format

  • A list of state strings.

Sample

print(circuit_states(["fail","fail","ok","ok"], 2, 1))

Output:

['closed', 'open', 'closed', 'closed']

Example:

Input:
print(circuit_states(["fail","fail","ok","ok"], 2, 1))
Output:
['closed', 'open', 'closed', 'closed']
Reasoning:
  • Index 0 ("fail"): The breaker starts in closed state. The consecutive failure count increments to 1. Since 1<21 < 2 (the threshold), the state remains closed.
  • Index 1 ("fail"): The failure count increments to 2. Since 2β‰₯22 \ge 2, the breaker trips to open and records opened_at = 1.
  • Index 2 ("ok"): The state is open. We check the cooldown condition: iβˆ’opened_at=2βˆ’1=1i - \text{opened\_at} = 2 - 1 = 1. Since 1β‰₯11 \ge 1 (the cooldown), the breaker transitions to half_open to probe. The outcome is "ok", so the probe succeeds, and the state resets to closed with the failure count reset to 0.
  • Index 3 ("ok"): The state is closed. The outcome is "ok", so the failure count remains 0. The state stays closed.
  • The final output is ['closed', 'open', 'closed', 'closed']

Constraints:

  • States are "closed", "open", "half_open".
  • Trip after fail_threshold consecutive failures.
  • From open, after cooldown elapsed indices, the next outcome is a half-open probe.
πŸ”’

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Circuit Breaker Transitions Over an Event Stream - Medium | PixelBank