DSA
    intermediate

    Interval Scheduling

    Interval Scheduling: Merge Intervals

    Master the Merge Intervals pattern: sort by start time, then sweep once to merge every overlapping range into the minimal covering set. Shows up constantly in FAANG interviews.

    ⏱ O(n log n)
    💾 O(n)
    Ready
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    Input
    [1,3]
    [2,6]
    [8,10]
    [15,18]
    [9,12]
    [5,7]
    Merged
    Input intervalConsideringMerged result

    Merge Intervals vs Activity Selection

    Merge Intervals (this topic): combine overlapping ranges into a minimal covering set — nothing is discarded, everything is merged. Sort by start time. Activity Selection (Greedy Algorithms topic): select the maximum count of non-overlapping intervals, discarding conflicts. Sort by end time. Both sweep linearly after the sort.

    Input: 6 intervals with [start, end] time ranges. Goal: merge all overlapping intervals into the minimal covering set.

    50%
    Live Variables
    n6

    Merge the intervals

    Schematic

    Merge [1,3], [2,6], [8,10], [9,12], [15,18]. Fill in the merged result in order.

    Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.

    merged 1
    merged 2
    merged 3
    3 pieces left to place

    Test your knowledge

    20 questions
    Question 1 of 20
    Score: 0

    What is the first step in the Merge Intervals algorithm?

    Write the code

    Runs your solution

    Merge Intervals

    Given an array of intervals where intervals[i] = [startᵢ, endᵢ], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.

    Example Cases

    Input:[[1,3],[2,6],[8,10],[15,18]]
    Output:[[1,6],[8,10],[15,18]]
    Input:[[1,4],[4,5]]
    Output:[[1,5]]
    Input:[[1,4],[0,4]]
    Output:[[0,4]]
    javascript

    The short version

    Picture a wall calendar covered in overlapping sticky notes representing scheduled meetings. Your goal is to consolidate every note that shares at least one minute with a neighbour into a single, bigger note — leaving only non-touching blocks behind. You pick up notes in order of their start time and ask: "Does this note touch or overlap the last block on my wall?" If yes, you extend that block (merge). If no, you pin a new block. One pass, done.

    In more depth

    Merging Intervals

    An interval is a pair [start, end] — a meeting, a booking, a range of line numbers. Interval problems ask how a set of them overlaps: merge the overlapping ones, count how many rooms are needed, or find the largest set that never conflicts.

    Why sorting comes first

    Unsorted, deciding whether any two intervals overlap is O(n²). Sorted by start time, the problem collapses: while sweeping left to right you only ever need to compare the current interval against the one you are building, because anything that could overlap it starts at or after the current position.

    The merge sweep

    Hold an open interval. For each next interval, if its start is at or before the open interval's end, the two touch — extend the open end to the larger of the two ends. Otherwise the open interval is finished: emit it and open a new one. One sort plus one pass gives O(n log n), dominated by the sort.

    The variants share the skeleton

    Sort by end time instead and greedily keep the earliest-finishing compatible interval, and you have maximum non-overlapping scheduling. Sweep starts and ends as separate +1/−1 events and the running total is the number of rooms needed at any moment.

    Interview Questions

    Google
    Meta
    Amazon
    Amazon
    Microsoft
    Google
    Meta
    Facebook
    LinkedIn
    Uber
    Apple
    Stripe

    Love this visualization?

    Sign up to track your progress, earn achievements, use the AI assistant, and access your personal learning dashboard.

    100% free • No credit card required