Merge Intervals
Updated
Problem statement
The input intervals is a list of pairs. A pair [start, end] has two integers with start <= end and covers each point between them, including start and end themselves. Pairs appear in no particular order.
Whenever two intervals have a point in common, join them into a single interval that covers both. Sharing only an endpoint is enough: [1, 2] and [2, 3] join into [1, 3]. Keep joining while any two intervals still have a point in common.
Return the joined intervals ordered by start, lowest first.
Function
merge(intervals: int[][]) → int[][]Examples
Example 1
intervals = [[4, 7], [1, 3], [2, 5], [9, 10]]return = [[1, 7], [9, 10]]In order of start the pairs are [1, 3], [2, 5], [4, 7] and [9, 10]. [1, 3] and [2, 5] have the points 2 to 3 in common, so they become [1, 5]. That one shares 4 to 5 with [4, 7], giving [1, 7]. [9, 10] begins after 7, so it remains separate.
Example 2
intervals = [[3, 6], [10, 12], [6, 8]]return = [[3, 8], [10, 12]][3, 6] ends at 6, the same point where [6, 8] begins. Meeting at one end counts as overlapping, so the two become [3, 8]. [10, 12] has no point in common with it.
Example 3
intervals = [[5, 12], [13, 15], [6, 8], [9, 9]]return = [[5, 12], [13, 15]][6, 8] and the single point [9, 9] lie inside [5, 12], so they add nothing and [5, 12] keeps its end 12. [13, 15] starts at 13, after 12, so the two have no point in common and both stay.
Constraints
1 <= intervals.length <= 104intervals[i].length == 20 <= intervals[i][0] <= intervals[i][1] <= 104
More Patreon Phone Screen questions
- Sliding-Window API Rate LimiterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Employee Importance from a DictionaryPHONE SCREEN · Seen Feb 2026
- Uniform Random Number from Coin TossesPHONE SCREEN · Seen Feb 2026
- Wordle Guess CheckerPHONE SCREEN · Seen Jan 2026
- Org Chart Report CountsPHONE SCREEN · Seen Dec 2025
- Format the Missing Numbers from 1 to 99PHONE SCREEN · Seen Sep 2025
- Pythagorean Triplets in an ArrayPHONE SCREEN · Seen Oct 2024
- LRU Cache with TTLPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2022
- Password GeneratorPHONE SCREEN · ONSITE INTERVIEW · Seen Mar 2022
- JSON ParserPHONE SCREEN · Seen Jun 2021
Similar questions at other companies
- Top Ten Largest FilesGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Valid Triangle NumberBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Travelers per Consecutive City TripletUber · ONSITE INTERVIEWUber interview questions
- Implement QuicksortByteDance · PHONE SCREENByteDance interview questions
- Merge Overlapping IntervalsShnoor International · ONSITE INTERVIEW
- Generate All Permutations of a NumberFleetx · ONSITE INTERVIEW
- Rank Word-Translation Hints from Sentence TranslationsDuolingo · PHONE SCREEN · ONSITE INTERVIEWDuolingo interview questions
- Top K Frequent ElementsAmazon · OA · PHONE SCREEN · ONSITE INTERVIEW
- Fair Capped Money DistributionGusto · PHONE SCREEN · ONSITE INTERVIEWGusto interview questions
- Group AnagramsAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions