The Find Right Interval interview question is an interval-matching problem. You are given a set of intervals . For each interval , you need to find the "right interval" such that and is minimized. You should return an array of indices , or -1 if no such interval exists.
Microsoft and Google use the Find Right Interval coding problem to assess your ability to use Binary Search on a custom dataset. It requires you to decouple the "index" from the "value" and sort the data to enable efficient searching. It tests your proficiency with Sorting interview patterns and searching for the "Lower Bound" of a value.
This problem is solved using Sorting and Binary Search.
(start_time, original_index).start_time.end_time:
bisect_left or lower_bound) on the sorted start_time list to find the first interval whose start_time is current end_time.Intervals: [[3, 4], [2, 3], [1, 2]] (Indices: 0, 1, 2)
(1, 2), (2, 1), (3, 0). Sorted by start.[3, 4]: Need start . None. Result -1.[2, 3]: Need start . Found (3, 0). Result 0.[1, 2]: Need start . Found (2, 1). Result 1.
Final Result: [-1, 0, 1].When you need to find the "smallest value X" in an array, your first thought should always be Sorting + Binary Search. It is the standard way to reduce a quadratic complexity to .