FastPrepDetect a Silent Sensor
Problem · Array

Detect a Silent Sensor

Learn this problem
MediumConfluent logoConfluentFULLTIMEONSITE INTERVIEW

Problem 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) → boolean

Examples

Example 1

pingTimestamps = [0,30,180]queryTime = 0return = true

Slots 1 and 2 are inactive, but no run of three consecutive inactive slots exists.

Example 2

pingTimestamps = [0,20,240]queryTime = 0return = false

Slots 1, 2, and 3 contain no ping, so the sensor is down.

Example 3

pingTimestamps = [59,60,179,300]queryTime = 0return = true

The 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.
  • pingTimestamps is sorted in nondecreasing order.
  • 0 <= queryTime <= 999999700.

More Confluent problems

drafts saved locally
public boolean wasAlive(int[] pingTimestamps, int queryTime) {
    // Write your code here.
}
pingTimestamps[0,30,180]
queryTime0
expectedtrue
checking account