Skip to the code
cf.
+25 XP

#114 Closest Timestamps Across Two Logs

medium

To line up a client log with a server log, an engineer looks for the moment when the two logs are closest in time. client and server are arrays of timestamps in milliseconds, each sorted ascending.

Write closestGap(client, server) that returns the smallest value of Math.abs(client[i] - server[j]) over all pairs. Both arrays have at least one item.

Example 1

Input: client = [100,350,900], server = [200,340,1500]
Output: 10

350 and 340 are 10 ms apart, the closest pair.

Example 2

Input: client = [5], server = [5]
Output: 0

The same moment appears in both logs.

Constraints

  • 1 <= client.length, server.length <= 100000
  • Both arrays are sorted ascending.
  • 0 <= timestamps <= 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).