By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
(Complete Guide for FAANG-Level Interviews)
"This pattern appears in 1 out of every 3 onsite interviews—nail it, and you prove you can handle real-world scheduling, resource allocation, and overlapping constraints with clarity and efficiency."
Before diving in, ensure you’re comfortable with: 1. Sorting (custom comparators, stability, time complexity). 2. Greedy Algorithms (optimal substructure, making locally optimal choices). 3. Two-Pointer Technique (traversing sorted arrays efficiently).
intervals.sort(key=lambda x: x[0])
current.end >= next.start
i
j
bisect
Mental Checklist for Every Interval Problem:
a.end >= b.start
What’s the output? (Merged intervals? Max non-overlapping count? Insertion result?)
Sort Intervals
Exception: For event-based problems (e.g., meeting rooms), sort all start/end points.
Initialize a Result Container
For counting: Use a counter or a stack.
Iterate with a Pointer
Compare the current interval with the next one to decide whether to merge, skip, or add.
Handle Edge Cases
Non-overlapping intervals.
Optimize for Follow-Ups
Problem: Given a list of intervals, merge all overlapping ones. Input: [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]]
[[1,3],[2,6],[8,10],[15,18]]
[[1,6],[8,10],[15,18]]
result = [intervals[0]]
i = 1
n-1
result[-1].end >= intervals[i].start
result[-1].end
intervals[i]
result
def merge(intervals): if not intervals: return [] intervals.sort(key=lambda x: x[0]) result = [intervals[0]] for i in range(1, len(intervals)): last = result[-1] current = intervals[i] if last[1] >= current[0]: # Overlap last[1] = max(last[1], current[1]) # Merge else: result.append(current) return result
Problem: Insert a new interval into a list of non-overlapping intervals (already sorted by start time). Input: intervals = [[1,3],[6,9]], newInterval = [2,5] Output: [[1,5],[6,9]]
intervals = [[1,3],[6,9]]
newInterval = [2,5]
[[1,5],[6,9]]
result = []
i = 0
intervals[i].end < newInterval.start
intervals[i].start <= newInterval.end
newInterval
def insert(intervals, newInterval): result = [] i = 0 n = len(intervals) # Add all intervals before the newInterval while i < n and intervals[i][1] < newInterval[0]: result.append(intervals[i]) i += 1 # Merge overlapping intervals while i < n and intervals[i][0] <= newInterval[1]: newInterval[0] = min(newInterval[0], intervals[i][0]) newInterval[1] = max(newInterval[1], intervals[i][1]) i += 1 result.append(newInterval) # Add remaining intervals while i < n: result.append(intervals[i]) i += 1 return result
Problem: Given intervals representing meeting times, find the minimum number of rooms required. Input: [[0,30],[5,10],[15,20]] Output: 2 (Rooms needed: [0,30] and [5,10] overlap, [15,20] overlaps with [0,30].)
[[0,30],[5,10],[15,20]]
2
rooms
start[i] < end[j]
rooms++
j++
def minMeetingRooms(intervals): if not intervals: return 0 starts = sorted([i[0] for i in intervals]) ends = sorted([i[1] for i in intervals]) rooms = 0 i = j = 0 while i < len(intervals): if starts[i] < ends[j]: rooms += 1 i += 1 else: j += 1 return rooms
a.end > b.start
>=
[1,2]
[2,3]
len(intervals) <= 1
"Here’s your 60-second cheat sheet for interval problems: 1. Sort first—by start time, end time, or both (for event-based problems). 2. Use a single pass—merge, count, or insert with two pointers. 3. Handle overlaps—if current.end >= next.start, merge them. 4. Watch edge cases—empty input, single interval, all overlapping. 5. Optimize for follow-ups—think heaps for dynamic updates, binary search for insertion.
Interval problems test your ability to handle real-world constraints. Sort, sweep, and merge—you’ve got this!
Now go crush that interview! ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.