Problem statement
Write function solution(events, threshold, cooldownMs). Inputs and outputs are JSON values. Do not modify input arrays or objects. External libraries, network access, and asynchronous functions are not supported. 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. Input limits: at most 50,000 items per input array and 1,000 characters per string. Numbers are finite; integers and accumulated integer results stay in the JavaScript safe-integer range. Other types and validity rules follow the statement above.
Example input and output
Example 1
Function arguments
[
[
{
"timestamp": 0,
"ok": false
},
{
"timestamp": 1,
"ok": false
},
{
"timestamp": 2,
"ok": true
},
{
"timestamp": 11,
"ok": true
}
],
2,
10
]Return value
[
{
"accepted": true,
"state": "closed"
},
{
"accepted": true,
"state": "open"
},
{
"accepted": false,
"state": "open"
},
{
"accepted": true,
"state": "closed"
}
]Example 2
Function arguments
[
[
{
"timestamp": 0,
"ok": false
},
{
"timestamp": 10,
"ok": false
},
{
"timestamp": 20,
"ok": true
}
],
1,
10
]Return value
[
{
"accepted": true,
"state": "open"
},
{
"accepted": true,
"state": "open"
},
{
"accepted": true,
"state": "closed"
}
]Example 3
Function arguments
[
[
{
"timestamp": 0,
"ok": false
},
{
"timestamp": 10,
"ok": false
},
{
"timestamp": 10,
"ok": true
}
],
1,
10
]Return value
[
{
"accepted": true,
"state": "open"
},
{
"accepted": true,
"state": "open"
},
{
"accepted": false,
"state": "open"
}
]