Drill

ProblemsJavaScript › data

Count the oversized gaps

mediumdataJavaScript

A service watchdog flags any two consecutive events that arrived further apart than a threshold.

timestampsGaps(timestamps: list<int>, gapThreshold: int) → int

Solve it in the editor →

Where you start

function timestampsGaps(timestamps, gapThreshold) {
  
}

Worked examples

CallResult
timestampsGaps([0,10,11,40], 10)1
timestampsGaps([0,10,11,40], 5)2
timestampsGaps([0,5,10], 5)0
timestampsGaps([1,100], 50)1

Hint

Compare each entry with the one before it and add to the count when the gap is too wide.

Reference solution in JavaScript
function timestampsGaps(timestamps, gapThreshold) {
  let count = 0;
  for (let i = 1; i < timestamps.length; i++) {
    if (timestamps[i] - timestamps[i - 1] > gapThreshold) count++;
  }
  return count;
}

The same problem in another language

More data problems in JavaScript