Master Interval & Range Interview Problems: Complete Guide
Learn to solve interval and range problems with merge intervals, insert interval, meeting rooms, and more.
20+ years shipping production code across the stack, with years spent interviewing engineers. Written from production experience, not tutorials.
- ✓Basic understanding of arrays and lists
- ✓Familiarity with sorting algorithms (O(n log n))
- ✓Knowledge of heap data structure (for Meeting Rooms II)
Interval problems involve ranges like [start, end]. Key techniques: sort by start time, then merge overlapping intervals. Common patterns: merge intervals, insert interval, meeting rooms, interval intersection, and minimum intervals to remove.
Think of intervals as time slots on a calendar. If two meetings overlap, you might need to merge them into one longer slot. Sorting by start time helps you process them in order, just like scanning your day chronologically.
Interval and range problems are a staple in coding interviews, especially at top tech companies. They test your ability to handle overlapping ranges, optimize scheduling, and manage edge cases. From merging calendar events to finding free time slots, these problems have real-world applications in scheduling, resource allocation, and data processing. In this guide, we'll cover the most common interval patterns, provide step-by-step solutions with time and space complexity, and share tips to ace your interview. Whether you're a beginner or looking to brush up, this tutorial will equip you with the tools to tackle any interval problem confidently.
1. Merge Intervals
The classic interval problem: given a collection of intervals, merge all overlapping intervals. The key is to sort by start time, then iterate. If the current interval overlaps with the last merged interval (i.e., current.start <= last.end), update the end to max(last.end, current.end). Otherwise, add the last merged interval to the result and start a new one. Finally, add the last interval after the loop. This runs in O(n log n) due to sorting, and O(n) space for the output.
2. Insert Interval
You are given a set of non-overlapping intervals sorted by start time, and a new interval to insert. Merge if necessary. Traverse the list and handle three cases: intervals that end before the new interval starts (add directly), intervals that start after the new interval ends (add new interval then the rest), and overlapping intervals (merge by updating start and end). This is O(n) time and O(n) space.
3. Meeting Rooms (Leetcode 252)
Determine if a person can attend all meetings. Given an array of meeting time intervals, return true if no two meetings overlap. Simply sort by start time and check if any meeting starts before the previous ends. O(n log n) time, O(1) space.
4. Meeting Rooms II (Leetcode 253)
Find the minimum number of conference rooms required. Use a min-heap to track end times of ongoing meetings. Sort intervals by start time. For each meeting, if the room with the earliest end time is free (end <= start), reuse it; otherwise, allocate a new room. The heap size at the end is the answer. O(n log n) time, O(n) space.
5. Interval List Intersections (Leetcode 986)
Given two lists of closed intervals, each sorted by start time, return the intersection of these interval lists. Use two pointers. For each pair, the intersection is [max(start1, start2), min(end1, end2)] if start <= end. Then advance the pointer with the smaller end. O(m+n) time, O(k) space for output.
6. Non-overlapping Intervals (Leetcode 435)
Given a collection of intervals, find the minimum number of intervals to remove to make the rest non-overlapping. Sort by end time (greedy). Keep track of the last end. If current start < last end, it overlaps and we remove it (increment count). Otherwise, update last end. O(n log n) time, O(1) space.
The Overlapping Calendar Bug
- Always sort intervals by start time before merging.
- Handle edge cases like equal start times and fully contained intervals.
- Test with intervals that touch (e.g., [1,2] and [2,3]) – they should not merge unless specified.
- Use a list to build the result incrementally.
intervals.sort(key=lambda x: x[0])if current_start <= last_end: merge| File | Command / Code | Purpose |
|---|---|---|
| merge_intervals.py | def merge(intervals): | 1. Merge Intervals |
| insert_interval.py | def insert(intervals, newInterval): | 2. Insert Interval |
| meeting_rooms.py | def canAttendMeetings(intervals): | 3. Meeting Rooms (Leetcode 252) |
| meeting_rooms_ii.py | def minMeetingRooms(intervals): | 4. Meeting Rooms II (Leetcode 253) |
| interval_intersection.py | def intervalIntersection(A, B): | 5. Interval List Intersections (Leetcode 986) |
| non_overlapping_intervals.py | def eraseOverlapIntervals(intervals): | 6. Non-overlapping Intervals (Leetcode 435) |
Key takeaways
Interview Questions on This Topic
Given a list of intervals, merge all overlapping intervals.
Frequently Asked Questions
20+ years shipping production code across the stack, with years spent interviewing engineers. Written from production experience, not tutorials.
That's Coding Patterns. Mark it forged?
3 min read · try the examples if you haven't