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
C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.
Where you start
public int TimestampsGaps(List<int> timestamps, int gapThreshold) {
}
Worked examples
| Call | Result |
|---|---|
TimestampsGaps(new List<int> { 0, 10, 11, 40 }, 10) | 1 |
TimestampsGaps(new List<int> { 0, 10, 11, 40 }, 5) | 2 |
TimestampsGaps(new List<int> { 0, 5, 10 }, 5) | 0 |
TimestampsGaps(new List<int> { 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 C#
public int TimestampsGaps(List<int> timestamps, int gapThreshold) {
int count = 0;
for (int i = 1; i < timestamps.Count; i++) {
if (timestamps[i] - timestamps[i - 1] > gapThreshold) count++;
}
return count;
}