Snowflake | Phone | Best Interval
Anonymous User
6406

Got this question during the phone interview:

Given a list of intervals, and a list of integers, calculate the best interval for each number in that list of intervals.

Best intervals in defined as the shortest length interval that covers the number (e.g, [1.10] and [2,5] both cover number 4, but [2.5] is the best interval among these two).
Also, the given list of intervals does not partially overlap, they are either disjoint or one fully overlaps another (see the example below)

Example:
Input:
intervals = {{2, 3}, {1, 20}, {15, 16}, {2, 5}, {1, 8}, {9, 12}, {6, 8}}
nums = {3, 5, 7, 9, 15, 17, 40}
Output:
{{2, 3}, {2, 5}, {6, 8}, {9, 12}, {15, 16}, {1, 20}, {}}

Explanation:
for 3, these intervals cover it: {2,3}, {1,20}, {2,5}, {1,8}, and {2,3} is the one has shortest length (=1), so output {2,3} for 3

Provided a "sweep-line" like algorithm, interviewer seems OK, but really not sure what is the best possible solution in terms of time complexity.

Comments (11)