Skip to main content
PatternIntervalsPatternsAlgorithms

Sort the intervals. Everything else is easy.

Master the interval pattern -- sorting, merging, overlapping detection, and the scheduling variants that come up in every interview loop.

Fin·Reviewed by Leo Kwan·Apr 3, 2026·4 min read
StrongYes tip

Nearly every interval problem starts with the same move: sort by start time. The real skill is knowing when to sort by end time instead.

An interval is a pair of numbers that mark a range, like a meeting from 9 to 10. Interview rounds test whether you can spot the range pattern and pick the right sort key. Most answers collapse to sort-by-start.

Interval problems show up constantly because they model real things: meetings, time slots, ranges, bookings. Nearly every interval problem starts with the same move -- sort by start time.

The Core Insight

Sort by start time. Process left to right. Every interval problem is a variation of this.

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

Once sorted, consecutive intervals can only relate in three ways:

Diagram
Rendering diagram...
  1. No overlap -- current starts after previous ends
  2. Overlap -- current starts before previous ends
  3. Containment -- one is entirely within the other

The Key Problems

Merge Intervals

LC 56 · The most-asked interval problem. Sort by start, merge overlapping.

PYTHON
def merge(intervals): intervals.sort() merged = [intervals[0]] for start, end in intervals[1:]: if start <= merged[-1][1]: merged[-1][1] = max(merged[-1][1], end) else: merged.append([start, end]) return merged

After sorting, you only compare each interval with the last merged one. That's what makes it O(n) after the sort.

Meeting Rooms II

LC 253 · Minimum number of meeting rooms needed. This is where a min-heap enters.

PYTHON
import heapq def minMeetingRooms(intervals): intervals.sort() heap = [] for start, end in intervals: if heap and heap[0] <= start: heapq.heappop(heap) heapq.heappush(heap, end) return len(heap)

The heap tracks the earliest-ending meeting. If the next meeting starts after that end time, reuse the room.

Insert Interval

LC 57 · Insert a new interval into a sorted, non-overlapping list. Three phases: add intervals before, merge overlapping, add intervals after. Don't -- a single linear scan is cleaner and what interviewers expect.

Non-overlapping Intervals

LC 435 · Minimum intervals to remove so the rest don't overlap. Sort by end time (not start), then greedily keep non-conflicting intervals. Sorting by end is counterintuitive but correct -- it maximizes the count by preferring shorter intervals.

When Interviewers Ask This

Interview QuestionLC #Sort ByTechniqueWhat They Want to Hear
"Merge overlapping intervals"56StartLinear merge"Compare only with last merged interval"
"Insert into non-overlapping list"57Pre-sortedThree-phase scan"No binary search needed -- linear is cleaner"
"Minimum meeting rooms"253StartMin-heap on end times"Heap tracks earliest free room"
"Min intervals to remove"435EndGreedy keep"Sort by END time, not start"
"Interval list intersection"986StartTwo pointers"Advance whichever ends first"
"Min arrows to burst balloons"452EndGreedy (same as 435)"This is non-overlapping intervals in disguise"
Diagram
Rendering diagram...

The sort-by-end variant trips people up. If they ask about maximizing non-overlapping intervals or activity selection, always sort by end time.

Common Mistakes

  • Not sorting first. If the interviewer gives you pre-sorted intervals, that's a deliberate hint. Don't re-sort.
  • Sort by start when you should sort by end. Non-overlapping Intervals and Activity Selection need end-time sort. Merge Intervals and Meeting Rooms use start-time sort. Know the difference.
  • Not drawing the intervals. Sketch a timeline on the whiteboard. Three overlapping intervals drawn on a number line reveals edge cases that staring at code won't.
  • Using < instead of <= for overlap checks. [1,2] and [2,3] -- do they overlap? Depends on the problem. Clarify with the interviewer.

Practice Problems

  1. Merge Intervals (LC 56) -- the foundation
  2. Meeting Rooms (LC 252) -- simple overlap check
  3. Meeting Rooms II (LC 253) -- min-heap for room counting
  4. Insert Interval (LC 57) -- three-phase linear scan
  5. Non-overlapping Intervals (LC 435) -- with end-time sort
  6. Minimum Number of Arrows (LC 452) -- non-overlapping intervals in disguise

Practice Intervals.

Explain your thinking like you're in the interview.

Practice with Fin or Coco

Sources

Source note

Fin and Coco are StrongYes editorial personas from the Council of Ternary Vertices — a trinary-star animal civilization that studies Earth's coding-interview process. Anecdotes map animal-universe experience to human interview mechanics; they are NEVER human-career claims. External citations link to public primary sources.

StrongYes editorial guide distilled from interview coaching notes, pattern walkthroughs, and curated prep plans used across the StrongYes library.

Reviewed by Leo Kwan on Apr 12, 2026.

Practice Intervals.

Reading builds recognition. Explaining builds recall. Run these problems with Fin or Coco.