Skip to main content

LeetCode Patterns

Intervals and Event Sweep Pattern

The Intervals and Event Sweep pattern converts a set of [start, end] intervals into a stream of +1 / -1 events sorted by time, then sweeps once to compute concurrency, conflict, or free-time questions. Sorting dominates at O(n log n); the sweep itself is O(n). It is the canonical move for meeting-room capacity, calendar conflicts, and 'find the gaps' problems.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Complexity

Time
O(n log n)
Space
O(n)

When to use Intervals and Event Sweep

  • You need to know the maximum number of intervals that overlap at any single point in time (peak concurrency).
  • You are asked whether any two intervals from a set conflict — sort by start, then check adjacent pairs in one pass.
  • You must insert a new interval into a sorted set of non-overlapping intervals and merge any neighbors it now touches.
  • The problem asks for the minimum number of intervals to remove so the rest are non-overlapping (interval scheduling).
  • You need the free time across multiple employees' schedules — merge all busy intervals first, then walk the gaps.

Template

function minMeetingRooms(intervals) {
  const starts = intervals.map(i => i[0]).sort((a, b) => a - b);
  const ends = intervals.map(i => i[1]).sort((a, b) => a - b);
  let rooms = 0;
  let maxRooms = 0;
  let s = 0;
  let e = 0;
  while (s < starts.length) {
    if (starts[s] < ends[e]) {
      rooms++;
      maxRooms = Math.max(maxRooms, rooms);
      s++;
    } else {
      rooms--;
      e++;
    }
  }
  return maxRooms;
}

Example LeetCode problems

#ProblemDifficultyFrequency
252Meeting Roomseasyfoundational
253Meeting Rooms IImediumcompany favorite
57Insert Intervalmediumfrequently asked
435Non-overlapping Intervalsmediumfrequently asked
759Employee Free Timehardcompany favorite
452Minimum Number of Arrows to Burst Balloonsmediumclassic
1094Car Poolingmediumfrequently asked
729My Calendar Imediumclassic

Common mistakes

  • Treating `[a, b]` and `[b, c]` as overlapping. Most LeetCode interval problems define touching endpoints as non-overlapping; double-check the prompt before deciding `<` vs `<=`.
  • Sorting by start time on Non-overlapping Intervals. The canonical move is to sort by END time — sorting by start drops you into a harder DP. End-time sort is the greedy unlock.
  • On Meeting Rooms II, using a single max-heap of end times but forgetting to push every new start (or to pop only when the current start is strictly greater than the heap top).
  • Reading sweep events in the wrong order when start and end coincide. If an end event ties with a start, process the end first so the same room/agent can be reused.
  • On Insert Interval, mutating the input array while iterating — collect output into a fresh array and return it, otherwise the indices drift mid-loop.

Frequently asked questions

What is the difference between Merge Intervals and Interval Sweep?

Merge Intervals collapses an existing list into a minimal non-overlapping set — a single sort then linear merge. Interval Sweep is broader: it answers concurrency and conflict questions by treating each interval as a +1 / -1 event pair, so it can compute peak overlap, schedule capacity, or free time across many sources.

Why does the meeting-room problem use two sorted arrays instead of one?

Splitting into a sorted starts array and a sorted ends array lets you sweep both with two pointers, counting rooms in use without a heap. Each time a start beats the next end, a new room opens; each time an end is reached first, an existing room closes. It is an O(n log n) sort plus an O(n) sweep, no priority queue needed.

When should I reach for a min-heap on intervals?

Use a min-heap of end times when you need to know which interval ends next at every step — typical for problems where you also need to attribute each new start to a specific resource (room, server, runway). The two-pointer trick handles peak-count problems; the heap handles attribution problems.

How do I handle 'insert interval into a sorted, non-overlapping list'?

Walk the existing list in three phases: copy intervals that end strictly before the new one starts, merge every interval that overlaps the new one (expand its bounds in place), then copy the rest. One pass, O(n) time, no sorting needed because the input is already ordered.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

Practice these live with InterviewChamp.AI

Drill the Intervals and Event Sweep pattern under real interview conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →