Intersect Multiple Interval Lists
Updated
Problem statement
You are given intervalLists, an array of lists of closed integer intervals. An interval [start, end] includes both endpoints. A list may be unsorted and may contain overlapping intervals.
First normalize every list by sorting and merging its overlapping intervals. Then return all maximal nonempty closed intervals that occur in every normalized list, sorted by start.
If intervalLists contains no lists or any list is empty, return an empty array.
Follow-up
How would you process each already-normalized list as a stream when the complete lists do not fit in memory?
Function
intersectIntervalLists(intervalLists: int[][][]) → int[][]Examples
Example 1
intervalLists = [[[0,2],[5,10],[13,23]],[[1,5],[8,12],[15,24]]]return = [[1,2],[5,5],[8,10],[15,23]]The result contains every closed overlap shared by the two lists, including the single-point interval [5,5].
Example 2
intervalLists = [[[1,4],[3,7]],[[2,5]],[[0,3],[6,9]]]return = [[2,3]]The first list normalizes to [[1,7]]. The interval shared by all three normalized lists is [2,3].
Example 3
intervalLists = [[[1,2]],[]]return = []No interval can occur in every list when one list is empty.
Constraints
- Each interval has exactly two endpoints
[start, end]withstart <= end. - Every endpoint fits in a signed
32-bit integer. - The returned intervals are maximal, pairwise disjoint, and sorted by start.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Meeting Rooms IIAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Meeting Rooms IIUber · PHONE SCREENUber interview questions
- Meeting Rooms IIWalmart · PHONE SCREENWalmart interview questions
- Most Employees Working at OnceCitadel · OACitadel interview questions
- Minimum Machines for TasksExpedia · OAExpedia interview questions
- Earliest Common Meeting SlotAdobe · PHONE SCREENAdobe interview questions
- Employee Free TimeIDFC Bank · ONSITE INTERVIEWIDFC Bank interview questions
- Generalized N-SumBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Merge IntervalsApple · OA · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
- Merge Sorted Squares of Two ArraysWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions