Drill

ProblemsPython › monitoring

The longest silence between heartbeats

mediummonitoringPython

A service sends a heartbeat every so often. The longest gap between two of them is how long it might have been down without anyone noticing.

longest_gap(timestamps: list<int>) → int

Solve it in the editor →

Where you start

def longest_gap(timestamps: list[int]) -> int:
    

Worked examples

CallResult
longest_gap([100, 130, 200, 205])70
longest_gap([205, 100, 200, 130])70
longest_gap([10, 20])10
longest_gap([42])0

Hint

Sort first. Without that, "next to each other" means nothing.

Reference solution in Python
def longest_gap(timestamps: list[int]) -> int:
    if len(timestamps) < 2:
        return 0
    s = sorted(timestamps)
    return max(s[i] - s[i - 1] for i in range(1, len(s)))

The same problem in another language

More monitoring problems in Python