Intervals
Interval problems involve working with ranges [start, end] and are common in scheduling, calendar, and resource allocation scenarios. The key is often sorting followed by a linear scan.Core Concepts
Interval Representation
- Inclusive: [start, end] - both endpoints included
- Half-open: [start, end) - start included, end excluded
- Tuple/Array: [start, end] or (start, end)
Common Operations
- Overlap: Two intervals intersect
- Merge: Combine overlapping intervals
- Insert: Add new interval and merge
- Intersection: Find common parts
Key Patterns & Techniques
1. Sort by Start Time
- Sort intervals by start time
- Linear scan to merge/process
- Time: O(n log n) for sort + O(n) for scan
2. Merge Overlapping
- Check if current overlaps with last merged
- Extend end time if overlapping
- Add new interval if not overlapping
3. Interval Intersection
- Two pointers on two interval lists
- Find overlap: max(start1, start2), min(end1, end2)
- Advance pointer with earlier end
4. Meeting Rooms
- Sort by start time
- Use min heap for end times
- Track concurrent meetings
Common Approaches
Merge Intervals
Insert Interval
Interval List Intersections
Meeting Rooms II (Min Rooms Needed)
Non-overlapping Intervals (Min Removals)
Problems in This Category
016. Merge Intervals
Medium | Frequency: 77.9%Sort by start, merge overlapping intervals in linear scan.
088. Interval List Intersections
Medium | Frequency: 32.0%Two pointers to find overlaps between two sorted interval lists.
077. Car Pooling
**Medium” | Frequency: 40.7%Track capacity at each location using interval scheduling.
Complexity Characteristics
| Pattern | Time Complexity | Space Complexity | Key Insight |
|---|---|---|---|
| Merge Intervals | O(n log n) | O(n) | Sort by start, linear merge |
| Insert Interval | O(n) | O(n) | Three phases: before, merge, after |
| Interval Intersection | O(m + n) | O(k) | Two pointers, k = output size |
| Meeting Rooms | O(n log n) | O(n) | Min heap tracks room availability |
| Min Removals | O(n log n) | O(1) | Sort by end, greedy selection |
Interview Tips
Pattern Recognition
- “Merge overlapping…” → Sort by start, scan and merge
- “Insert interval” → Three-phase approach
- “Minimum rooms/resources” → Heap of end times
- “Intersection of intervals” → Two pointers
- “Non-overlapping intervals” → Sort by end, greedy
- “Point in time queries” → Sweep line algorithm
Common Mistakes
- Not sorting intervals first
- Wrong overlap condition (use
<=vs<) - Forgetting to handle edge cases (empty, single interval)
- Not considering touching intervals [1,2] and [2,3]
- Off-by-one errors in interval boundaries
Key Insights
- Sort first - enables O(n) scan after O(n log n) sort
- Overlap condition:
current.start <= last.end - Merge by extending end:
max(last.end, current.end) - Sort by end for greedy - scheduling problems
- Heap tracks concurrent events - meeting rooms
- Two pointers for two lists - intersection problems
Overlap Detection
Two intervals overlap if:
Types of Overlap
Advanced Patterns
Sweep Line Algorithm
Range Module (Interval Set)
Employee Free Time
Decision Tree
Pro Tip: Almost all interval problems start with sorting. Sort by start time for most problems, but sort by end time for scheduling/greedy problems (like minimum removals). The overlap condition is
current.start <= last.end.