Problem · Array
Detect a Silent Sensor
Learn this problemProblem statement
You are given the nondecreasing ping timestamps for one sensor and a query time queryTime.
Inspect five consecutive half-open time slots of sixty seconds each:
- slot 0 is
[queryTime, queryTime + 60), - slot 1 is
[queryTime + 60, queryTime + 120), - and so on through slot 4.
A slot is active when it contains at least one ping. Return false if any three consecutive slots are inactive; otherwise return true.
The input array has already been selected from a per-sensor index, so pings belonging to other sensors are outside this function's contract.
Function
wasAlive(pingTimestamps: int[], queryTime: int) → booleanExamples
Example 1
pingTimestamps = [0,30,180]queryTime = 0return = trueSlots 1 and 2 are inactive, but no run of three consecutive inactive slots exists.
Example 2
pingTimestamps = [0,20,240]queryTime = 0return = falseSlots 1, 2, and 3 contain no ping, so the sensor is down.
Example 3
pingTimestamps = [59,60,179,300]queryTime = 0return = trueThe pings at 59, 60, and 179 belong to slots 0, 1, and 2 respectively. The ping at 300 lies outside the five-slot window.
Constraints
0 <= pingTimestamps.length <= 200000.0 <= pingTimestamps[i] <= 1000000000.pingTimestampsis sorted in nondecreasing order.0 <= queryTime <= 999999700.