📖 Scenario: Imagine you have a calendar with booked time slots represented as intervals. You want to add a new meeting time and keep the calendar sorted and merged so no overlapping meetings exist.
🎯 Goal: Build a program that inserts a new interval into a sorted list of non-overlapping intervals and merges any overlapping intervals.
📋 What You'll Learn
Create a list called intervals with sorted, non-overlapping intervals as lists of two integers.
Create a list called new_interval representing the interval to insert.
Write code to insert new_interval into intervals and merge overlapping intervals.
Print the final list of merged intervals.
💡 Why This Matters
🌍 Real World
Calendars, booking systems, and timeline management often require inserting and merging time intervals to avoid conflicts.
💼 Career
Understanding interval merging is useful for software engineers working on scheduling apps, event planners, and resource allocation tools.
Progress0 / 4 steps
1
Create the initial sorted list of intervals
Create a list called intervals with these exact intervals: [1, 3], [6, 9].
DSA Python
Hint
Use a list of lists to represent intervals.
2
Create the new interval to insert
Create a list called new_interval with the exact values [2, 5].
DSA Python
Hint
Just assign the list [2, 5] to new_interval.
3
Insert and merge the new interval
Write code to insert new_interval into intervals and merge overlapping intervals. Use a list called merged to store the result. Iterate over intervals + [new_interval] sorted by start time. Merge intervals if they overlap.
DSA Python
Hint
Sort all intervals including the new one, then merge overlapping ones by comparing the end of the last merged interval with the start of the current.
4
Print the merged intervals
Print the list merged to show the final merged intervals after insertion.
DSA Python
Hint
Use print(merged) to display the final intervals.
Practice
(1/5)
1. What is the main goal when inserting a new interval into a sorted list of non-overlapping intervals?
easy
A. To add the new interval and merge any overlapping intervals to keep the list sorted and non-overlapping
B. To replace the first interval with the new interval regardless of overlap
C. To insert the new interval at the end without checking for overlaps
D. To delete all intervals that come after the new interval
Solution
Step 1: Understand the problem requirement
The goal is to insert a new interval into a sorted list of intervals without overlaps.
Step 2: Merge overlapping intervals after insertion
After inserting, merge any intervals that overlap to maintain the sorted, non-overlapping property.
Final Answer:
To add the new interval and merge any overlapping intervals to keep the list sorted and non-overlapping -> Option A
Quick Check:
Insert and merge intervals = To add the new interval and merge any overlapping intervals to keep the list sorted and non-overlapping [OK]
Hint: Insert then merge overlapping intervals to keep list sorted [OK]
Common Mistakes:
Ignoring merging after insertion
Inserting without maintaining order
Replacing intervals incorrectly
2. Which of the following Python code snippets correctly inserts a new interval into a sorted list of intervals without merging?
easy
A. intervals.remove(new_interval)
B. intervals.append(new_interval)
C. intervals.sort()
D. intervals.insert(2, new_interval)
Solution
Step 1: Identify correct insertion method
Using insert(index, element) places the new interval at the correct position without losing order.
Step 2: Understand other options
append adds at end without order, sort only sorts but doesn't insert, remove deletes an element.
Final Answer:
intervals.insert(2, new_interval) -> Option D
Quick Check:
Use insert() to add at specific position [OK]
Hint: Use insert(index, value) to place interval correctly [OK]
Common Mistakes:
Using append without sorting
Using remove instead of insert
Relying on sort without insertion
3. What is the output of the following code?
intervals = [[1,3],[6,9]]
new_interval = [2,5]
# Insert new_interval and merge
result = []
i = 0
while i < len(intervals) and intervals[i][1] < new_interval[0]:
result.append(intervals[i])
i += 1
while i < len(intervals) and intervals[i][0] <= new_interval[1]:
new_interval[0] = min(new_interval[0], intervals[i][0])
new_interval[1] = max(new_interval[1], intervals[i][1])
i += 1
result.append(new_interval)
while i < len(intervals):
result.append(intervals[i])
i += 1
print(result)
medium
A. [[2, 5], [6, 9]]
B. [[1, 3], [2, 5], [6, 9]]
C. [[1, 5], [6, 9]]
D. [[1, 3], [6, 9], [2, 5]]
Solution
Step 1: Add intervals before new_interval
The first while loop checks if intervals[i][1] < new_interval[0]. For i=0, 3 < 2 is false, so no intervals added before. result = [].
Step 2: Merge overlapping intervals
Intervals overlapping with new_interval [2,5] are [1,3] and [2,5] merged to [1,5].
Step 3: Add remaining intervals
Remaining interval [6,9] is added after merged interval.