Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
Two services log the same user actions, but their clocks drift, so the same action is stamped a few seconds apart in each log. Before building a full matcher, the platform team wants the smallest skew the two logs show: the pair of events — one from each log — whose timestamps are closest together, which is the best candidate for the same action seen twice. Both logs are already sorted by time.
Write closest_pair(log_a, log_b). It takes two lists of integer epoch-second timestamps, each sorted ascending, and returns [a, b, gap]: a timestamp from log_a, a timestamp from log_b, and abs(a - b), for the pair with the smallest gap.
Function to write
closest_pair(log_a: list[int], log_b: list[int]) -> list[int] | None[a, b, gap] for the closest cross-log pair (ties to the smallest a, then b), or None if a log is empty.
How to approach it
Walk both logs at once and always advance the pointer at the smaller timestamp — moving the larger one can only make the gap wider. Compare candidates on (gap, a, b).
Sample cases
+ 3 held back until you submit
drifting clocks, one close match
The closest pair is 1125 and 1118, seven seconds apart, found deep in both logs.
Input
Argument 1
[
1000,
1060,
1125,
1190
]Argument 2
[
1031,
1118,
1250
]Returns
[
1125,
1118,
7
]a tie on the gap
Three pairs are 30 seconds apart; the tie-break keeps the one with the smallest a.
Input
Argument 1
[
200,
260
]Argument 2
[
230,
290
]Returns
[
200,
230,
30
]the second log is empty
With no events in one log there is no cross-log pair, so the answer is None.
Input
Argument 1
[
500,
600
]Argument 2
[] (empty list)
Returns
None
the clocks agree exactly
75 appears in both logs, so the gap is 0 and the walk can stop early.
Input
Argument 1
[
40,
75,
90,
130
]Argument 2
[
10,
75,
75,
200
]Returns
[
75,
75,
0
]Constraints
log_a and log_b are lists of integers sorted ascending. Timestamps may repeat within a log.a must come from log_a and b from log_b; two events from the same log never form a pair.[a, b, gap] where gap is abs(a - b) and is the smallest gap over all cross-log pairs.a; if that still ties, the smallest b.None.Worked example
Take log_a = [1000, 1060, 1125, 1190] and log_b = [1031, 1118, 1250]. Start both pointers at the front. 1000 vs 1031: gap 31, best so far; 1000 is smaller, and moving the larger one would only widen the gap, so advance log_a. 1060 vs 1031: gap 29, new best; advance log_b. 1060 vs 1118: gap 58. Advance log_a. 1125 vs 1118: gap 7, new best. Advance log_b. 1125 vs 1250: 125. Advance log_a. 1190 vs 1250: 60. log_a is used up, so stop: the answer is [1125, 1118, 7].
The trap: in [200, 260] against [230, 290], three pairs are 30 apart. A nested loop that replaces the best on <= returns [260, 290, 30]; the contract says the smallest a wins, so the answer is [200, 230, 30].
What this tests
Two pointers across two sorted lists, where each step must choose which pointer can still improve the answer. It is the classic 'smallest difference between two sorted arrays' problem. Data engineering candidates at Oracle report being asked to find pairs of numbers with the minimum absolute difference, which is this problem's close cousin. The target is O(n + m) time and O(1) space; the nested loop is O(n·m) and passes the samples but not two logs of a million events. A data engineer meets this aligning events from services with skewed clocks — the same idea as an as-of join or pandas merge_asof.
closest_pair(log_a: list[int], log_b: list[int]) -> list[int] | NoneSubmit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.