0Pricing
Coding Interview Prep · Lesson

Merge Overlapping Intervals

Combine ranges that touch or overlap.

Merge Overlapping Intervals is a free Coding Interview Prep lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Coding Interview Prep learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Merge Goal

Given many intervals, you want to merge the ones that touch or overlap into the fewest possible non-overlapping ranges. 🧩

When Two Intervals Overlap

Two intervals overlap when one starts before the other ends. After sorting by start, that means the next start is at or below the current end.

Sort First, Always

Merging only works left to right if intervals are in order, so begin by sorting them by start. This is the foundation of the whole sweep.

intervals.sort(key=lambda x: x[0])

Keep a Current Range

Walk the sorted list while holding one current merged interval. Each new interval either extends it or starts a fresh range.

Extend on Overlap

If the next start is within the current range, they overlap, so you stretch the current end to the larger of the two ends.

cur_end = max(cur_end, end)

Take the Max End

Always use max for the new end. A short interval nested inside a long one must not shrink the range you already built.

Close and Open a New One

If the next start is past the current end, there is a gap. Push the finished range to your answer and begin a new current interval.

result.append([cur_start, cur_end])

Don't Forget the Last One

The loop builds the final range but never pushes it. After the loop ends, append that last current interval so it is not lost.

Touching Counts as Overlap

Decide if [1, 3] and [3, 5] should merge. Usually they do, so use start <= cur_end. Read the problem to confirm this edge rule.

The Full Sweep

One pass after sorting gives you all merged ranges, so the whole method runs in O(n log n) from the sort plus a linear sweep.

for s, e in intervals[1:]:
    if s <= cur_end:
        cur_end = max(cur_end, e)
    else:
        result.append([cur_start, cur_end]); cur_start, cur_end = s, e

A Common Use

Merging powers calendars and booking systems: collapse busy blocks to see real free time. Many contest tasks hide this same shape.

Quick Check

You merge intervals after sorting by start.

Recap

Sort by start, hold a current range, and extend with max on overlap or push and reset on a gap. Remember the final append. 🚀

Frequently asked questions

Is the “Merge Overlapping Intervals” lesson free?

Yes — the full text of “Merge Overlapping Intervals” is free to read here on the web, and the Coding Interview Prep course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Coding Interview Prep course, upgrade to CoddyKit PRO.

What will I learn in “Merge Overlapping Intervals”?

Combine ranges that touch or overlap. You practise Coding Interview Prep with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Coding Interview Prep?

No prior experience is required. Coding Interview Prep on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Merge Overlapping Intervals” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Coding Interview Prep lesson?

Yes. Every Coding Interview Prep lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Sort Intervals by Start
  2. Merge Overlapping Intervals
  3. Line Sweep for Max Overlap
  4. Minimum Removals for No Overlap
← Back to Coding Interview Prep