Find Containing Range
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates the ability to design appropriate data structures and apply algorithmic reasoning for efficient interval containment queries, emphasizing preprocessing, correctness, and time-space trade-offs.
Constraints
- 0 <= len(ranges) <= 200000
- 0 <= len(queries) <= 200000
- Each range is `[l, r]` where `l <= r`
- Range endpoints and query values are integers in the range [-10^9, 10^9]
- The input ranges are inclusive and pairwise non-overlapping
Examples
Input: ([[1, 3], [50, 100]], [2, 50, 10])
Expected Output: [[1, 3], [50, 100], None]
Explanation: 2 lies in [1, 3], 50 lies in [50, 100], and 10 is not inside any range.
Input: ([[5, 8], [-10, -5], [0, 0]], [-7, 0, 8, 9, -11])
Expected Output: [[-10, -5], [0, 0], [5, 8], None, None]
Explanation: The ranges must be sorted first. -7 is in [-10, -5], 0 is in the single-point range [0, 0], 8 is in [5, 8], while 9 and -11 are outside all ranges.
Input: ([], [1, -1, 0])
Expected Output: [None, None, None]
Explanation: With no ranges, every query returns None.
Input: ([[2, 2]], [2, 1, 3])
Expected Output: [[2, 2], None, None]
Explanation: The range [2, 2] contains only the value 2.
Input: ([[100, 200], [-3, -1], [10, 20]], [-1, 10, 200, 21, 10])
Expected Output: [[-3, -1], [10, 20], [100, 200], None, [10, 20]]
Explanation: Queries can repeat. Boundary values like -1, 10, and 200 are included because the ranges are inclusive.
Hints
- If the ranges are not sorted, sort them by their starting value first.
- For each query x, find the rightmost range whose start is less than or equal to x, then check whether x is at most that range's end.