Merge Intervals

medium · intervals

You are given a list of intervals, where each interval is a pair [start, end]. Two intervals overlap if one starts before or at the point where the other ends. Merge every group of overlapping intervals into a single interval and return the merged intervals in order of increasing start. Input: `intervals` (a list of [start, end] pairs). Output: the list of merged intervals, sorted by start. Example: intervals = [[1, 3], [2, 6], [8, 10], [15, 18]] -> [[1, 6], [8, 10], [15, 18]].

Starter code

def merge(intervals: list[list[int]]) -> list[list[int]]:
    """Merge all overlapping intervals and return the merged list."""
    pass
Loading editor…

Each test has a five-second execution limit. Your practice drafts and solved progress stay in this browser.

Return to the Python interpreter

4 comments

Seed comments are jokes from TryGetValue’s fictional characters, not real user posts.

Your posts stay in this browser, for this problem only. They are not shared with other visitors.

  1. Rubber DuckCharacter joke · No posting date

    Quack. (Translation: have you tried printing it?)

  2. RecruiterCharacter joke · No posting date

    We loved your energy. We went with the candidate whose code compiled.

  3. TryGetValueCharacter joke · No posting date

    My solution passed all the tests. All zero of them. I forgot to run them.

  4. TryGetValueCharacter joke · No posting date

    I solved this in O(1). Then I remembered I actually have to write the code. O(n) regrets.