DSA Tracker

Medium

Non-overlapping Intervals

A medium Intervals problem included in Love Babbar 450, Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.

Topic
Intervals
Sheets
2
Core for
6 roles
Platform
LeetCode

The problem

Given a collection of intervals, find the minimum number of intervals you need to remove so that the remaining intervals do not overlap.

Example 1

Input
intervals = [[1,2],[2,3],[3,4],[1,3]]
Output
1
Why
Removing [1,3] leaves [[1,2],[2,3],[3,4]] which do not overlap.

Example 2

Input
intervals = [[1,2],[1,2],[1,2]]
Output
2
Why
Only one [1,2] can remain; remove two.

Constraints

  • 1 <= intervals.length <= 10^4
  • intervals[i].length == 2
  • -5 * 10^4 <= intervals[i][0] < intervals[i][1] <= 5 * 10^4

How to think about it

Updated 2026-09-09

Minimizing removals is the exact dual of maximizing how many mutually disjoint intervals can survive. The survivor that finishes earliest frees up the timeline the soonest for whoever comes next, so ordering by end coordinate guarantees you never regret keeping the first one you can grab.

Approaches, worst first

  1. Sort by start and branch

    time O(2^n) · space O(n)

    Sort by start time and, upon finding a collision, branch on whether to discard the current interval or the previous one. Exploring both choices branches exponentially unless memoized, and even dynamic programming over the sorted list wastes quadratic time.

  2. Greedy choice by end timeWrite this one

    time O(n log n) · space O(1)

    Sort ascending by end coordinate. Keep a pointer to the last accepted finish time; whenever the next start is strictly earlier than that finish, a collision is unavoidable and you must drop it. Otherwise, accept the interval and slide the finish pointer forward.

Where people lose marks · 3
  • Sorting by start coordinate instead of end coordinate. A long interval starting early can mask multiple short, valid intervals, tricking a greedy start-based sweep into poor choices.
  • Treating intervals that merely touch boundaries as overlapping. When an end matches the next start, they do not collide according to the definition and both can stay.
  • Negative coordinates can break custom comparators if differences cause integer underflow; write explicit comparison checks instead of subtraction.

The theory behind it

Intervals — the ground this problem stands on. All Intervals problems

What Intervals is

An interval is a continuous range of numbers defined by two values: a start point and an end point. Think of a calendar event booked from two to four o'clock, or a cut segment on a ruler. Because each interval covers every number between its boundaries, two intervals can sit apart with empty room between them, touch at an edge, or overlap across a shared span of numbers.

When to reach for it

Reach for intervals when the input consists of start and end pairs representing time slots, schedules, ranges, or geometric segments. Phrasings asking to merge overlapping blocks, insert a new meeting into a busy calendar, find the minimum number of conference rooms needed, or count how many intervals must be removed to eliminate overlaps all point directly to interval patterns. Whenever problems involve resource contention over time, think intervals.

How the pattern works

The opening move is almost always sorting the intervals by their start times, or occasionally by their end times. Once ordered, compare the current interval with the previous one. If the new start time is less than or equal to the previous end time, the two intervals collide; merge them by stretching the previous end time to the maximum of both ends. If they do not collide, the previous interval is finished, so append it to the result and start tracking the new one. For room counts, split intervals into separate start and end events.

What each operation costs

OperationTime
sort intervals by start or end timeO(n log n)
merge sorted intervals in a single passO(n)
track active meetings using a min-heapO(n log n)
What usually goes wrong with Intervals
  • Merging two overlapping intervals by taking the second interval end without using the maximum of both ends, which shrinks an interval when the first one completely swallowed the second.
  • Treating intervals that touch at the exact same boundary point as disjoint when the problem statement defines boundaries as closed and inclusive.
  • Forgetting to append the final merged interval to the output list after the iteration loop finishes.

Which roles need this problem

Intervals is a core topic for these 6 roles — if you're targeting one of them, this problem is early in your path, not optional.

Secondary for 11 more roles, including Frontend Engineer, Full-Stack Developer, SDET / QA Engineer.

Track this in your role's order

Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.

Start free

More Intervals problems

Problem set and role mapping as of .