LeetCopilot Logo
LeetCopilot
LeetCode Pattern/Greedy Algorithm/5 Common Interval Scheduling Mistakes (And How to Avoid Them)

5 Common Interval Scheduling Mistakes (And How to Avoid Them)

LeetCopilot Team
Dec 30, 2025
10 min read
Greedy AlgorithmInterval SchedulingCommon MistakesDebuggingInterview Prep
Learn the most common mistakes in interval scheduling problems and how to debug them. Master the pattern with a comprehensive debugging checklist.

Interval scheduling problems have recurring mistakes that trip up even experienced developers.

This guide covers the 5 most common mistakes and shows you exactly how to debug them.

TL;DR

5 Common Mistakes:

  1. Sorting by start time instead of end time
  2. Off-by-one in overlap detection
  3. Not handling edge cases
  4. Wrong comparison operator
  5. Not sorting at all

Quick fix: Use the template, test edge cases, verify sort criterion.

The Correct Template (Reference)

python
def interval_scheduling(intervals):
    """
    Correct template for interval scheduling.
    """
    if not intervals:
        return 0
    
    # MUST sort by END time
    intervals.sort(key=lambda x: x[1])
    
    count = 1
    last_end = intervals[0][1]
    
    for i in range(1, len(intervals)):
        start, end = intervals[i]
        
        # Use >= for non-overlap
        if start >= last_end:
            count += 1
            last_end = end
    
    return count

Mistake 1: Sorting by Start Time

The Error:

Wrong:

python
intervals.sort(key=lambda x: x[0])  # Sorting by START

Correct:

python
intervals.sort(key=lambda x: x[1])  # Sorting by END

Why it fails:

Sorting by start time doesn't guarantee optimal selection.

Counterexample:

code
Intervals: [[1, 100], [2, 3], [4, 5]]

Sort by start:
  [[1, 100], [2, 3], [4, 5]]
  Select [1, 100]
  Can't select [2, 3] or [4, 5] (overlap)
  Result: 1 interval ✗

Sort by end:
  [[2, 3], [4, 5], [1, 100]]
  Select [2, 3]
  Select [4, 5] (4 >= 3)
  Can't select [1, 100]
  Result: 2 intervals ✓

How to debug:

  1. Check your sort key: key=lambda x: x[1]
  2. Verify first interval selected has earliest end time
  3. Test with counterexample above

Mistake 2: Off-by-One in Overlap Detection

The Error:

Wrong:

python
if start > last_end:  # Should be >=
    count += 1

Correct:

python
if start >= last_end:  # Touching is OK
    count += 1

Why it matters:

Depends on problem definition:

  • Non-overlapping: start >= last_end (touching allowed)
  • Strictly non-overlapping: start > last_end (gap required)

Example:

code
Intervals: [[1, 2], [2, 3]]

Using >:
  Select [1, 2]
  2 > 2? NO
  Result: 1 interval

Using >=:
  Select [1, 2]
  2 >= 2? YES
  Result: 2 intervals

Most LeetCode problems use >= (touching is non-overlapping).

How to debug:

  1. Read problem carefully: "non-overlapping" vs "strictly non-overlapping"
  2. Test with touching intervals: [[1, 2], [2, 3]]
  3. Check expected output

Mistake 3: Not Handling Edge Cases

The Error:

Wrong:

python
def interval_scheduling(intervals):
    intervals.sort(key=lambda x: x[1])
    count = 1  # Assumes at least 1 interval
    last_end = intervals[0][1]  # Crashes if empty!
    ...

Correct:

python
def interval_scheduling(intervals):
    if not intervals:  # Handle empty
        return 0
    
    intervals.sort(key=lambda x: x[1])
    count = 1
    last_end = intervals[0][1]
    ...

Common edge cases:

  1. Empty array: intervals = []
  2. Single interval: intervals = [[1, 2]]
  3. All overlapping: intervals = [[1, 5], [2, 3], [3, 4]]
  4. None overlapping: intervals = [[1, 2], [3, 4], [5, 6]]
  5. Touching intervals: intervals = [[1, 2], [2, 3]]

How to debug:

  1. Add if not intervals: return 0 at start
  2. Test all 5 edge cases above
  3. Verify single interval returns 1

Mistake 4: Wrong Comparison Operator

The Error:

Wrong:

python
if start > last_end:  # Wrong direction
    count += 1
    last_end = end

Multiple issues:

  • Should be >= not > (see Mistake 2)
  • Logic might be inverted

Correct logic:

python
if start >= last_end:  # No overlap
    count += 1
    last_end = end
# else: skip this interval (overlaps)

How to debug:

  1. Trace through example manually
  2. Check: "If start >= last_end, we CAN select"
  3. Verify last_end updates only when selecting

Mistake 5: Not Sorting at All

The Error:

Wrong:

python
def interval_scheduling(intervals):
    # Forgot to sort!
    count = 1
    last_end = intervals[0][1]
    
    for i in range(1, len(intervals)):
        ...

Why it fails:

Greedy choice only works on sorted data.

Example:

code
Intervals: [[4, 5], [1, 2], [3, 4]]

Without sort:
  Select [4, 5]
  1 >= 5? NO
  3 >= 5? NO
  Result: 1 interval ✗

With sort (by end):
  [[1, 2], [3, 4], [4, 5]]
  Select [1, 2]
  Select [3, 4] (3 >= 2)
  Can't select [4, 5] (4 >= 4 but end=5)
  Result: 2 intervals ✓

How to debug:

  1. Check for .sort() call before loop
  2. Print intervals after sort to verify
  3. Test with unsorted input

Debugging Checklist

When your solution fails, check these in order:

code
□ 1. Is array sorted?
   └─ intervals.sort(key=lambda x: x[1])

□ 2. Sorting by END time?
   └─ key=lambda x: x[1], not x[0]

□ 3. Using >= for comparison?
   └─ if start >= last_end

□ 4. Handling empty input?
   └─ if not intervals: return 0

□ 5. Initializing count correctly?
   └─ count = 1 (first interval always selected)

□ 6. Updating last_end correctly?
   └─ last_end = end (when selecting)

□ 7. Loop starting at index 1?
   └─ for i in range(1, len(intervals))

Complete Correct Solution

python
def eraseOverlapIntervals(intervals):
    """
    LeetCode #435: Non-overlapping Intervals
    Return minimum removals to make non-overlapping.
    
    Time: O(n log n), Space: O(1)
    """
    if not intervals:
        return 0
    
    # Sort by end time
    intervals.sort(key=lambda x: x[1])
    
    # Count non-overlapping intervals
    count = 1
    last_end = intervals[0][1]
    
    for i in range(1, len(intervals)):
        start, end = intervals[i]
        
        if start >= last_end:  # No overlap
            count += 1
            last_end = end
    
    # Minimum removals = total - non-overlapping
    return len(intervals) - count

Testing Strategy

Test these cases:

python
# Test 1: Empty
assert eraseOverlapIntervals([]) == 0

# Test 2: Single interval
assert eraseOverlapIntervals([[1, 2]]) == 0

# Test 3: No overlap
assert eraseOverlapIntervals([[1, 2], [3, 4]]) == 0

# Test 4: All overlap
assert eraseOverlapIntervals([[1, 5], [2, 3], [3, 4]]) == 2

# Test 5: Touching (non-overlapping)
assert eraseOverlapIntervals([[1, 2], [2, 3]]) == 0

# Test 6: Classic case
assert eraseOverlapIntervals([[1, 2], [2, 3], [3, 4], [1, 3]]) == 1

Common Variations

Variation 1: Count Non-Overlapping

python
# Return count of non-overlapping intervals
return count

Variation 2: Count Removals

python
# Return minimum removals
return len(intervals) - count

Variation 3: Touching Intervals

Problem: Arrows to burst balloons (touching counts as overlap)

python
if start > last_end:  # Use > instead of >=
    count += 1

Practice these to master the pattern:

  1. Non-overlapping Intervals (LC #435) - Classic
  2. Minimum Arrows (LC #452) - Touching variant
  3. Meeting Rooms (LC #252) - Feasibility check
  4. Meeting Rooms II (LC #253) - Count resources
  5. Merge Intervals (LC #56) - Combine overlapping

Quick Reference Card

code
INTERVAL SCHEDULING CHEAT SHEET

1. Sort by: END time (x[1])
2. Compare: start >= last_end
3. Edge case: if not intervals: return 0
4. Initialize: count = 1
5. Update: last_end = end (when selecting)
6. Loop: range(1, len(intervals))

Common bugs:
✗ Sort by start time
✗ Use > instead of >=
✗ Forget to sort
✗ Don't handle empty
✗ Initialize count = 0

Conclusion

Most interval scheduling bugs come from these 5 mistakes:

  1. Wrong sort (start vs end)
  2. Wrong operator (> vs >=)
  3. Missing edge cases (empty array)
  4. Wrong comparison (inverted logic)
  5. No sort (forgot entirely)

Debug strategy:

  1. Use the checklist
  2. Test edge cases
  3. Trace manually
  4. Verify sort criterion

Master this pattern, and interval problems become routine.

For more, see Interval Scheduling Template and Why Sort by End Time.

Want to Practice LeetCode Smarter?

LeetCopilot is a free browser extension that enhances your LeetCode practice with AI-powered hints, personalized study notes, and realistic mock interviews — all designed to accelerate your coding interview preparation.

Also compatible with Edge, Brave, and Opera

Related Tutorials