AI Nativeレベル別課題

レベル3 · 応用 · 構造と効率

サーキットブレーカーの実装

連続失敗と復帰時刻の境界を扱います。

問題文

function solution(events, threshold, cooldownMs)を書いてください。入出力はJSON値です。入力配列やオブジェクトを変更しないでください。外部ライブラリ・ネットワーク・非同期関数は使えません。 events:{timestamp:nonnegative integer,ok:boolean}[] in nondecreasing time; threshold:positive integer; cooldownMs:nonnegative integer. Initially closed with zero failures. Reject while timestamp<openUntil without changing state. At/after expiry reset to closed and zero failures before accepting. Accepted success resets failures; failure increments them and reaching threshold opens until timestamp+cooldownMs. Return {accepted,state:'open'|'closed'} after each event. Zero cooldown is valid. 入力範囲:各入力配列は最大50,000項目、文字列は最大1,000文字です。数値は有限値で、整数と整数の合算結果はJavaScriptの安全な整数範囲内です。その他の型と有効性は上記の規則に従います。

入出力例

例 1

関数の引数

[
  [
    {
      "timestamp": 0,
      "ok": false
    },
    {
      "timestamp": 1,
      "ok": false
    },
    {
      "timestamp": 2,
      "ok": true
    },
    {
      "timestamp": 11,
      "ok": true
    }
  ],
  2,
  10
]

戻り値

[
  {
    "accepted": true,
    "state": "closed"
  },
  {
    "accepted": true,
    "state": "open"
  },
  {
    "accepted": false,
    "state": "open"
  },
  {
    "accepted": true,
    "state": "closed"
  }
]
例 2

関数の引数

[
  [
    {
      "timestamp": 0,
      "ok": false
    },
    {
      "timestamp": 10,
      "ok": false
    },
    {
      "timestamp": 20,
      "ok": true
    }
  ],
  1,
  10
]

戻り値

[
  {
    "accepted": true,
    "state": "open"
  },
  {
    "accepted": true,
    "state": "open"
  },
  {
    "accepted": true,
    "state": "closed"
  }
]
例 3

関数の引数

[
  [
    {
      "timestamp": 0,
      "ok": false
    },
    {
      "timestamp": 10,
      "ok": false
    },
    {
      "timestamp": 10,
      "ok": true
    }
  ],
  1,
  10
]

戻り値

[
  {
    "accepted": true,
    "state": "open"
  },
  {
    "accepted": true,
    "state": "open"
  },
  {
    "accepted": false,
    "state": "open"
  }
]
Practice has no points, deadline or submission cap. Use AI and search freely. Sign in to run public examples, or verify locally.

88 / 20,000文字

Add your test · optional

Enter a JSON argument array in events, threshold, cooldownMs order. Your test runs alongside the three examples.

Save both files in one folder and run this command with Node.js. Read the checks field for individual example results.

node verify-circuit-breaker.mjs
문의하기

서비스명: 포트포올
문의는 운영자에게 이메일로 전송됩니다.

이메일과 문의 내용은 답변을 위해 운영자 및 메일 발송 서비스로 전송됩니다. 비밀번호·결제정보·민감한 개인정보는 입력하지 마세요.