Problems › TypeScript › data
Count the oversized gaps
A service watchdog flags any two consecutive events that arrived further apart than a threshold.
- Timestamps are ascending, so only neighbouring pairs need comparing.
- Count the pairs whose difference is strictly more than the threshold.
- The threshold is a positive gap above which a pair counts.
timestampsGaps(timestamps: list<int>, gapThreshold: int) → int
Where you start
function timestampsGaps(timestamps: number[], gapThreshold: number): number {
}
Worked examples
| Call | Result |
|---|---|
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 TypeScript
function timestampsGaps(timestamps: number[], gapThreshold: number): number {
let count = 0;
for (let i = 1; i < timestamps.length; i++) {
if (timestamps[i] - timestamps[i - 1] > gapThreshold) count++;
}
return count;
}