Every interval problem is decided before the first line of logic, by the choice of sort key. Pick the right one and the rest is a single greedy pass.
Intervals show up as meetings, bookings, ranges of IDs, time windows on a chart. Unsorted, comparing every pair is O(n²). Sorted, each interval only ever needs to be compared with the one thing you are carrying forward, and the pass is O(n). The interesting content of this pattern is which key to sort by, because different questions want different keys.
[start, end], meetings, bookings, flight times, version ranges.a and b overlap when a.start < b.end and b.start < a.end. Equivalently, they are disjoint when one ends before the other begins. If they are already sorted by start, so a.start ≤ b.start, this collapses to a single test: b.start < a.end.[1, 3] and [3, 5] conflict? For merging ranges of integers, usually yes, so the test is ≤. For meeting rooms, usually no, because a meeting ending at 3 frees the room for one starting at 3, so the test is <. This single character decides several of the problems below. Ask, do not guess.Reading the figure. The top rows are the intervals sorted by start. The bottom row is the merged result. Bars that overlap their neighbour fuse into one range. A gap between bars closes the range for good.
After sorting by start time, every interval yet to be processed starts at or after the current one. So if the next interval does not overlap the range being built, no later interval can overlap it either, since they all start even later. The current range is finished and can be emitted. This is the loop invariant that makes the sweep correct in one pass.
This list is the pattern. Everything else is bookkeeping.
PriorityQueue of ends.public static class SweepMergeTemplate
{
/// <summary>Union of overlapping ranges.</summary>
/// <param name="intervals">Pairs [start, end]. Sorted in place.</param>
/// <returns>Disjoint ranges in start order.</returns>
/// <example><c>SweepMerge([[1, 4], [2, 5], [7, 9], [8, 11]])</c>
/// returns [[1, 5], [7, 11]].</example>
public static int[][] SweepMerge(int[][] intervals)
{
if (intervals.Length == 0)
return []; // nothing to merge
// a[0]: the start. Sort by start so overlaps sit next to each other.
Array.Sort(intervals, (a, b) => a[0].CompareTo(b[0]));
// [0]: the first interval opens the output. Copy it so we never alias.
List<int[]> output = [[intervals[0][0], intervals[0][1]]];
// i: index of the incoming interval. Start at 1: index 0 is already in output.
// Invariant: output is the merged union of intervals[0..i-1].
for (int i = 1; i < intervals.Length; i++)
{
int start = intervals[i][0], end = intervals[i][1]; // [0] start, [1] end
int[] last = output[^1]; // ^1: the interval still being built
// last[1]: its end. A start at or before it means the two overlap.
if (start <= last[1]) // overlap or touch
last[1] = Math.Max(last[1], end); // Max: never shrink, it may sit inside
else
output.Add([start, end]); // a gap: start a new merged interval
}
return output.ToArray();
}
}
Use Math.Max(last[1], end), not end. A short interval fully inside a long one must not shrink it.
public static class MaxNonOverlappingTemplate
{
/// <summary>Largest set of mutually disjoint intervals.</summary>
/// <param name="intervals">Pairs [start, end]. Sorted in place.</param>
/// <returns>How many intervals can be kept with no overlap.</returns>
/// <example><c>MaxNonOverlapping([[1, 3], [2, 4], [3, 5], [6, 7]])</c> returns 3.</example>
public static int MaxNonOverlapping(int[][] intervals)
{
if (intervals.Length == 0)
return 0; // 0: no intervals, none can be kept
// a[1]: the end. Earliest finish first leaves the most room after it.
Array.Sort(intervals, (a, b) => a[1].CompareTo(b[1]));
int kept = 1; // 1: the earliest-finishing one is always kept
int lastEnd = intervals[0][1]; // [0][1]: end of that first kept interval
// i: index of the interval being judged. 1: index 0 is already kept.
// Invariant: kept is the best count for intervals seen, ending at lastEnd.
for (int i = 1; i < intervals.Length; i++)
{
if (intervals[i][0] >= lastEnd) // [0]: its start. No conflict with the last kept
{
kept++; // +1: take this interval too
lastEnd = intervals[i][1]; // [1]: its end, the new finish line to beat
}
}
return kept;
}
}
Reading the figure. Bars are drawn on a time axis in end order. Green bars are kept. The red dashed bar is dropped because it starts before lastEnd, the end of the last green bar.
public static class MaxConcurrentTemplate
{
/// <summary>Peak number of intervals alive at any instant.</summary>
/// <param name="intervals">Pairs [start, end].</param>
/// <returns>The largest number of intervals that overlap at one time.</returns>
/// <example><c>MaxConcurrent([[1, 5], [2, 6], [4, 8], [7, 9]])</c> returns 3.</example>
public static int MaxConcurrent(int[][] intervals)
{
var events = new List<(int Time, int Delta)>();
foreach (int[] iv in intervals)
{
events.Add((iv[0], +1)); // [0] start. +1: one more interval is alive
events.Add((iv[1], -1)); // [1] end. -1: one interval stops here
}
// At a tie, process the end (-1) before the start (+1), so a range that
// finishes exactly when another begins does not count as concurrent.
// Value tuples sort by Time first, then by Delta, and -1 sorts before +1.
events.Sort();
int running = 0, best = 0; // 0: nothing alive before the first event
// delta: +1 or -1 from above. The time is not needed once sorted.
// Invariant: running is how many intervals are alive just after this event.
foreach (var (_, delta) in events)
{
running += delta;
best = Math.Max(best, running); // keep the peak
}
return best;
}
}
C# value tuples compare item by item, so Array.Sort on (int Time, int Delta) does the tie-break for free, because -1 < +1. That is a neat detail worth pointing out in an interview.
Reading the figure. Blue bars are the intervals. Each start adds one and each end takes one away. The running row is the count after each event. The amber band is where three intervals overlap, the peak.
end instead of Math.Max(last[1], end) when merging. [[1, 10], [2, 3]] must merge to [1, 10], not [1, 3].< and ≤ for touching intervals. Ask once, then be consistent.merged.Add(intervals[0]) stores a reference to the caller’s array, and the merge then edits it in place. Copy with [first[0], first[1]] or (int[])first.Clone().a[0] - b[0]. The subtraction overflows for values far apart, such as int.MinValue and 1, and the sort goes wrong. Use a[0].CompareTo(b[0]).Array.Sort is an introsort and is not stable. None of these problems need stability. If one does, use OrderBy, which is stable.Given a list of intervals, merge all overlapping ones and return the non-overlapping intervals that cover exactly the same ground. For [[1,3],[2,6],[8,10],[15,18]] the answer is [[1,6],[8,10],[15,18]].
public static class MergeIntervals
{
/// <summary>Merge every set of overlapping intervals into one.</summary>
/// <param name="intervals">Pairs [start, end] with start <= end, in any order.
/// Sorted in place as a side effect.</param>
/// <returns>Disjoint intervals in increasing order covering the same union.</returns>
/// <example><c>Merge([[1, 3], [2, 6], [8, 10], [15, 18]])</c>
/// returns [[1, 6], [8, 10], [15, 18]].</example>
public static int[][] Merge(int[][] intervals)
{
if (intervals.Length == 0)
return []; // nothing to merge
// a[0]: the start. Sorted by start, any overlap is with the last merged one.
Array.Sort(intervals, (a, b) => a[0].CompareTo(b[0]));
// [0]: the first interval seeds the output. A fresh array, so no aliasing.
List<int[]> merged = [[intervals[0][0], intervals[0][1]]];
// i: index of the incoming interval. 1: index 0 is already in merged.
// Invariant: merged is the sorted, disjoint union of intervals[0..i-1].
for (int i = 1; i < intervals.Length; i++)
{
int start = intervals[i][0], end = intervals[i][1]; // [0] start, [1] end
int[] last = merged[^1]; // ^1: the interval still being built
// last[1]: its end. A start at or before it means overlap.
if (start <= last[1])
{
// Overlaps or touches. Extend, but never shrink: the incoming
// interval may sit entirely inside the one we are building.
last[1] = Math.Max(last[1], end);
}
else
{
merged.Add([start, end]); // a gap: open a new merged interval
}
}
return merged.ToArray();
}
}
[[1,3],[2,6],[8,10],[15,18]], already sorted by start:
[2, 6]. last is [1, 3]. 2 ≤ 3, so they overlap. last becomes [1, 6].[8, 10]. last is [1, 6]. 8 > 6, so they are disjoint. Emit and open [8, 10].[15, 18]. last is [8, 10]. 15 > 10, so they are disjoint. Emit and open [15, 18].Reading the figure. Amber is last, the range being built. Blue is the incoming interval. Green ranges are finished and emitted. An incoming start at or before the end of last extends it.
[[1, 10], [2, 3]]: must give [[1, 10]]. This is the max test.[[1, 4], [4, 5]]: with ≤ they merge to [[1, 5]]. Confirm which behaviour is wanted.You are given a list of already sorted, non-overlapping intervals and one new interval. Insert it, merging where needed, and return the result still sorted and non-overlapping.
while loops rather than one loop with flags is what makes it readable.public static class InsertInterval
{
/// <summary>Insert one interval into a sorted, disjoint list, merging as needed.</summary>
/// <param name="intervals">Sorted, pairwise non-overlapping [start, end] pairs.</param>
/// <param name="newInterval">The interval to add.</param>
/// <returns>A new array, still sorted and non-overlapping.</returns>
/// <example><c>Insert([[1, 3], [6, 9]], [2, 5])</c> returns [[1, 5], [6, 9]].</example>
public static int[][] Insert(int[][] intervals, int[] newInterval)
{
int start = newInterval[0], end = newInterval[1]; // [0] start, [1] end
var output = new List<int[]>();
int i = 0, n = intervals.Length; // 0: scan from the first interval
// Phase 1: everything that finishes before the new interval begins.
// [i][1]: the end of interval i. It ends before start, so no overlap.
while (i < n && intervals[i][1] < start)
{
output.Add(intervals[i]);
i++; // +1: move to the next interval
}
// Phase 2: everything that overlaps. Widen, then emit once.
// [i][0]: the start of interval i. At or before end means overlap.
while (i < n && intervals[i][0] <= end)
{
start = Math.Min(start, intervals[i][0]); // [0]: grow left to the earlier start
end = Math.Max(end, intervals[i][1]); // [1]: grow right to the later end
i++; // +1: this one is absorbed, move on
}
output.Add([start, end]);
// Phase 3: everything that starts after the merged interval ends.
while (i < n)
{
output.Add(intervals[i]);
i++; // +1: move to the next interval
}
return output.ToArray();
}
}
intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], inserting [4, 8]:
[1, 2]. 2 < 4, so it is copied.[3, 5], [6, 7], [8, 10]. The new interval grows 4→3 then 8→10, and emits [3, 10].[12, 16]. It is copied.Reading the figure. Blue bars are copied as they are. Amber is the new interval as it grows. Red dashed bars are absorbed into it. Green is the result, still sorted and non-overlapping.
Result: [[1,2],[3,10],[12,16]].
[2, 3] into [[1, 10]]: phase 2 widens it back out to [1, 10]. Verify this case, it is the one people get wrong.Given a list of intervals, return the minimum number to remove so that the rest are pairwise non-overlapping.
intervals.Length - kept.f be the interval with the earliest end time. Take any optimal solution OPT that does not contain f, and let g be the first interval in OPT. Since f ends no later than g, swapping g for f cannot create a new conflict with anything later in OPT. So there is an optimal solution containing f. Remove f and everything it conflicts with, and repeat the argument on what remains. Greedy by earliest end is therefore optimal.[[1, 100], [2, 3], [4, 5]]. Sorted by start, the greedy keeps [1, 100] first and then must drop both others, keeping 1. Sorted by end, it keeps [2, 3] and [4, 5], keeping 2. Have this counterexample ready. Interviewers ask for it.public static class EraseOverlapIntervals
{
/// <summary>Fewest intervals to delete so the remainder are pairwise disjoint.</summary>
/// <param name="intervals">Pairs [start, end]. Sorted in place as a side effect.</param>
/// <returns>The number of intervals that must be removed.</returns>
/// <example><c>MinRemovals([[1, 2], [2, 3], [3, 4], [1, 3]])</c> returns 1.</example>
public static int MinRemovals(int[][] intervals)
{
if (intervals.Length == 0)
return 0; // 0: nothing to remove
// Earliest finishing time first: it leaves the most room for what follows.
Array.Sort(intervals, (a, b) => a[1].CompareTo(b[1])); // a[1]: the end
int kept = 1; // 1: the earliest-finishing one is always kept
int lastEnd = intervals[0][1]; // [0][1]: end of that first kept interval
// i: index of the interval being judged. 1: index 0 is already kept.
// Invariant: kept is the most disjoint intervals among intervals[0..i-1].
for (int i = 1; i < intervals.Length; i++)
{
if (intervals[i][0] >= lastEnd) // touching is allowed, so >= not >
{
kept++; // +1: keep this one too
lastEnd = intervals[i][1]; // [1]: the new finish line to beat
}
}
// Removing the fewest is the same as keeping the most.
return intervals.Length - kept;
}
}
[[1,2],[2,3],[3,4],[1,3]] sorted by end becomes [[1,2],[1,3],[2,3],[3,4]]:
[1, 2]. No lastEnd yet. Keep, it is the seed. kept = 1.[1, 3]. lastEnd = 2. 1 < 2 is a conflict, so drop. kept = 1.[2, 3]. lastEnd = 2. 2 ≥ 2, so keep. kept = 2.[3, 4]. lastEnd = 3. 3 ≥ 3, so keep. kept = 3.Reading the figure. Rows are in end order. The amber line is lastEnd when that row is checked. A bar that starts left of the line overlaps and turns red. Green bars are kept.
Kept 3 of 4, so remove 1.
0.n - 1.0.>=. Confirm this reading with the interviewer.Given meeting time intervals, return the minimum number of conference rooms needed to hold them all. A meeting that ends at time t frees its room for a meeting starting at t.
PriorityQueue<int, int> of the end times of meetings in rooms. It is a min-heap, so the earliest end is on top. For each new meeting, reuse the earliest-ending room if it is already free. Otherwise open a new room. The heap size at the end is the answer.+1 at its start and a -1 at its end, sort all events, and track the running total. Simpler, and the one to reach for if the heap feels heavy.public static class MeetingRooms
{
/// <summary>Minimum rooms needed so no two meetings share a room.</summary>
/// <param name="intervals">Pairs [start, end] with start < end.
/// Sorted in place as a side effect.</param>
/// <returns>The peak number of simultaneously running meetings.</returns>
/// <example><c>MinRooms([[0, 30], [5, 10], [15, 20]])</c> returns 2.</example>
public static int MinRooms(int[][] intervals)
{
if (intervals.Length == 0)
return 0; // 0: no meetings, no rooms
// a[0]: the start. Hand out rooms in the order meetings begin.
Array.Sort(intervals, (a, b) => a[0].CompareTo(b[0]));
// Min-heap of end times. Element and priority are both the end time.
// The top is the room that frees up soonest.
var rooms = new PriorityQueue<int, int>();
// Invariant: rooms holds one end time per room opened so far.
foreach (int[] meeting in intervals)
{
int start = meeting[0], end = meeting[1]; // [0] start, [1] end
// Peek: the earliest end. At or before start means that room is free.
if (rooms.TryPeek(out _, out int earliest) && earliest <= start)
rooms.DequeueEnqueue(end, end); // reuse: pop the free room, push ours
else
rooms.Enqueue(end, end); // every room is busy, open one more
}
return rooms.Count; // rooms never close, so the count is the peak
}
}
DequeueEnqueue is one sift instead of the two you get from Dequeue followed by Enqueue. Same result, half the work, and it reads as the single action it is. Two more C# facts: PriorityQueue has no decrease-key, and enumerating it does not give priority order. Here the end time is both the element and the priority.public static class MeetingRoomsSweep
{
/// <summary>Same answer via a sweep over start and end events.</summary>
/// <param name="intervals">Pairs [start, end].</param>
/// <returns>The peak number of rooms in use at once.</returns>
/// <example><c>MinRooms([[0, 30], [5, 10], [15, 20]])</c> returns 2.</example>
public static int MinRooms(int[][] intervals)
{
// Two events per meeting. * 2: one start and one end each.
var events = new (int Time, int Delta)[intervals.Length * 2];
for (int i = 0; i < intervals.Length; i++)
{
events[2 * i] = (intervals[i][0], 1); // 2 * i: even slot. 1: room in use
events[2 * i + 1] = (intervals[i][1], -1); // + 1: odd slot. -1: room frees
}
// Sorting tuples puts (t, -1) before (t, 1), so a room freed at time t
// is available to a meeting starting at t. That is the tie-break we want.
Array.Sort(events);
int running = 0, best = 0; // 0: no rooms in use before the first event
// delta: 1 or -1 from above. The time is not needed once sorted.
// Invariant: running is the rooms in use just after this event.
foreach (var (_, delta) in events)
{
running += delta;
best = Math.Max(best, running); // keep the peak
}
return best;
}
}
[[0, 30], [5, 10], [15, 20]], heap version:
[0, 30]. The queue is empty. Open a room. Queue after: [30].[5, 10]. Earliest end 30. 30 > 5, still busy, so open. Queue after: [10, 30].[15, 20]. Earliest end 10. 10 ≤ 15, so reuse. Queue after: [20, 30].Reading the figure. Blue bars open a room. The green bar reuses a room, since the earliest end in the heap was 10 and 10 ≤ 15. The amber line marks time 15. The boxes show the PriorityQueue of end times after each meeting.
Two rooms.
Both solutions are dominated by the sort. The heap holds at most one entry per concurrent meeting.
0 rooms.[[1, 5], [5, 9]]: one room, because of ≤ in the heap version and the tuple tie-break in the sweep. This is the case that separates the two readings of “overlap”.n rooms.next.start < current.end. Everything else is bookkeeping.Math.Max(last[1], end), or nested intervals shrink the result.Pattern 5, Cyclic Sort, is the other “rearrange in place” pattern. Where intervals lean on sorting, cyclic sort exploits a much stronger promise: the values are drawn from a known bounded range, so each one already knows where it belongs.