Skip to the code
cf.
+50 XP

#207 Widest Quiet Period

hard

A server log has the times (in seconds) of every request in times, in no particular order. The on-call dashboard wants the widest quiet period: the biggest difference between two requests that are next to each other once the times are sorted.

Write widestGap(times) that returns that difference, or 0 if there are fewer than two requests. Aim for linear time: sorting first is too slow for the real log size.

Example 1

Input: times = [3,6,9,1]
Output: 3

Sorted: 1, 3, 6, 9. The gaps are 2, 3, and 3.

Example 2

Input: times = [10]
Output: 0

One request has no neighbor.

Constraints

  • 0 <= times.length <= 100000
  • 0 <= times[i] <= 10^9
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).