Drill

ProblemsPython › data

Count the oversized gaps

mediumdataPython

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

timestamps_gaps(timestamps: list<int>, gap_threshold: int) → int

Solve it in the editor →

Where you start

def timestamps_gaps(timestamps: list[int], gap_threshold: int) -> int:
    

Worked examples

CallResult
timestamps_gaps([0, 10, 11, 40], 10)1
timestamps_gaps([0, 10, 11, 40], 5)2
timestamps_gaps([0, 5, 10], 5)0
timestamps_gaps([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 Python
def timestamps_gaps(timestamps: list[int], gap_threshold: int) -> int:
    count = 0
    for i in range(1, len(timestamps)):
        if timestamps[i] - timestamps[i - 1] > gap_threshold:
            count += 1
    return count

The same problem in another language

More data problems in Python