Drill

ProblemsJava › data

Count the oversized gaps

mediumdataArraysJava

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

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

Java 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.

Solve it in Python →

Where you start

int timestampsGaps(List<Integer> timestamps, int gapThreshold) {
    
}

Worked examples

CallResult
timestampsGaps(Main.<Integer>ls(0, 10, 11, 40), 10)1
timestampsGaps(Main.<Integer>ls(0, 10, 11, 40), 5)2
timestampsGaps(Main.<Integer>ls(0, 5, 10), 5)0
timestampsGaps(Main.<Integer>ls(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 Java
int timestampsGaps(List<Integer> timestamps, int gapThreshold) {
    int count = 0;
    for (int i = 1; i < timestamps.size(); i++) {
        if (timestamps.get(i) - timestamps.get(i - 1) > gapThreshold) count++;
    }
    return count;
}

The same problem in another language

More data problems in Java