AW Dev Rethought

Code is read far more often than it is written - Guido van Rossum

🧩 Python DSA Patterns — Interval Patterns ⏱️🐍


Description:

Interval problems deal with ranges like [start, end] and focus on how these ranges overlap, merge, or conflict with each other.

They are extremely common in:

  • Scheduling
  • Calendars
  • Resource allocation
  • Timeline-based problems

✨ What Is an Interval?

An interval is usually represented as:

[start, end]

Examples:

  • Meetings: [9, 11]
  • Events: [2, 5]
  • Time slots, ranges, bookings

The core challenge is understanding how two intervals relate:

  • Do they overlap?
  • Can they be merged?
  • Do they conflict?

⚡ Golden Rule (Very Important)

Almost all interval problems start by sorting intervals by start time.

Once sorted, decisions become local and predictable.


🧠 Common Interval Patterns

1️⃣ Merge Overlapping Intervals

  • Combine intervals that overlap
  • Output a condensed list

2️⃣ Detect Overlap / Conflict

  • Check if two intervals clash
  • Used in scheduling & meeting rooms

3️⃣ Insert a New Interval

  • Insert and merge efficiently

4️⃣ Minimum Resources Required

  • Find how many rooms/resources are needed
  • Track overlaps using pointers or heaps

📝 Example 1 — Merge Intervals (Core Pattern)

Merge all overlapping intervals into one.

def merge_intervals(intervals):
    intervals.sort(key=lambda x: x[0])
    merged = []

    for start, end in intervals:
        if not merged or merged[-1][1] < start:
            merged.append([start, end])
        else:
            merged[-1][1] = max(merged[-1][1], end)

    print("Merged Intervals:", merged)

📝 Example 2 — Check If Intervals Overlap

Two intervals overlap if the start of one is before the end of the other.

def is_overlap(a, b):
    return a[0] < b[1] and b[0] < a[1]

print(is_overlap([1, 3], [2, 4]))  # True
print(is_overlap([1, 2], [3, 4]))  # False

📝 Example 3 — Meeting Rooms (Conceptual)

Find minimum rooms needed so meetings don’t overlap.

Idea:

  • Sort start times and end times separately
  • Use two pointers to track overlaps

(No full code here — this is a pattern note, not a script.)


🧠 Where Interval Patterns Help

  • 📅 Calendar & scheduling systems
  • 🏢 Meeting room allocation
  • 🚦 Time conflict detection
  • 📊 Resource usage over time
  • 🧩 Greedy scheduling problems

❗ Common Mistakes

  • Forgetting to sort intervals first
  • Confusing inclusive vs exclusive ends
  • Overcomplicating overlap logic
  • Trying brute-force comparisons (O(n²))

✅ Takeaways

  • Sort by start time first — always
  • Most interval problems are greedy
  • Merge when current start ≤ previous end
  • Interval patterns appear far more often than expected

🏋️ Practice Problems

  1. Merge Intervals
  2. Insert Interval
  3. Meeting Rooms I & II
  4. Non-overlapping Intervals
  5. Minimum Number of Arrows to Burst Balloons

Link copied!

Comments

Add Your Comment

Comment Added!