Skip to the content

The witnesses' clocks

Problem

"I heard knocking at minutes 12, 40 and 65," said the doorman. The cook, from across the courtyard, saw lights at minutes 30 and 61. Detective Shaw thinks a knock and a light very close together are the same event, and wants to know how close they got.

You get knocks and lights, two lists of minutes (integers from 0 up), in any order and possibly with repeats. Return an integer: the smallest distance between a minute in knocks and one in lights, no matter which comes first. The pair is always one knock with one light: two knocks close together don't count. Here you return 4, for 65 and 61. If a knock and a light fall on the same minute, return 0. If either list is empty, there is no pair: return -1.

Examples

  • The courtyard witnesses

    [12, 40, 65], [30, 61] → 4

  • Two close knocks don't count

    [10, 11], [50, 3] → 7

  • The same minute

    [5, 20, 33], [33, 1] → 0

  • Minutes of one to three digits

    [100, 7, 58], [9, 70, 120] → 2

  • Nobody saw lights

    [9], [] → -1

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def closest(knocks, lights):
    pass

JavaScript

function closest(knocks, lights) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(n log n) challenges

See all challenges →