Greedy Algorithms
Interval Problems
Master Greedy interval algorithms: Activity Selection end-time sorts, overlapping mergers, and insertions.
Last Updated: August 2, 2026
•
18 min read
1. Introduction
What are Interval Problems?
Interval Problems require organizing blocks of time (start and end times) on a 1D timeline. Common variants include:Why study them?
These problems are extremely common in coding assessments. They test your ability to sort structures according to various criteria (start time vs. end time) and manage coordinate edge overlaps.2. Mental Model: The Booking Desk
Imagine managing booking requests at a recording studio:
1:00 PM - 3:00 PM and client B books 2:00 PM - 5:00 PM, their sessions overlap. You must merge them into a single occupied block: 1:00 PM - 5:00 PM.3. Core Algorithms & Implementations
1. Merge Intervals (LeetCode 56)
≤ the end time of the last merged interval, they overlap. Merge them by updating the last merged interval's end time to max(last_merged.end, current.end).2. Activity Selection / Max Non-Overlapping (LeetCode 435 / 646)
≥ the last selected end time, select it and update the end time threshold.4. Visualizing Merging & Scheduling
Merging overlapping intervals [[1, 3], [2, 6], [8, 10]]:
5. Real-World Examples
6. Interview Perspective
How Interviewers Ask This Topic
Interviewers test sorting selection logic:O(N) time.Common Mistakes
Warning: 1. Wrong Sorting Key: Sorting by start-time for activity selection, or sorting by end-time for merging intervals. This breaks the greedy conditions, leading to bugs.
> 2. Off-by-one boundary checks: Using<instead of≤(or vice versa) for overlaps. If an interval ends at3and the next starts at3, they do not overlap (start >= last_end).
7. Summary
next.start <= last.end.next.start >= last.end.O(N log N) time (sorting bottleneck), O(N) space.8. Quiz
Question 1: If meeting intervals are [[1, 4], [2, 3]], and we merge them, what is the resulting interval?
Answer:[1, 4]. The second interval is entirely nested inside the first one. max(4, 3) = 4.
Question 2: Why does Activity Selection sort by end-times rather than start-times?
Answer: If we sort by start-times, an activity starting at9:00 AM and ending at 9:00 PM would be selected first, preventing us from scheduling multiple shorter activities that run throughout the day.
Question 3: What is the time complexity of the Insert Interval algorithm if the input is already sorted?
Answer:O(N) time, as we can insert the new interval and merge overlaps in a single linear pass without re-sorting the entire array.
Question 4: True or False: If two intervals [1, 5] and [5, 10] share boundary 5, they are considered overlapping in 'Merge Intervals'.
Answer: False (usually). They touch at boundary5 but do not overlap. However, check LeetCode problem specifications: standard definitions treat start <= last_end as overlapping, which would merge them into [1, 10].
Question 5: What is the space complexity of Merge Intervals?
Answer:O(N) space to store the results list of merged intervals.