0
0
DSA Pythonprogramming~10 mins

Insert Interval into Sorted List in DSA Python - Execution Trace

Choose your learning style9 modes available
Concept Flow - Insert Interval into Sorted List
Start with sorted intervals list
Create new interval node
Traverse list to find insert position
Check overlap with current interval?
YesMerge intervals
Update merged interval
Insert new or merged interval
Continue merging if needed
Finish traversal
Return updated intervals list
Start with a sorted list of intervals, find where to insert the new interval, merge overlapping intervals, and return the updated sorted list.
Execution Sample
DSA Python
intervals = [[1,3],[6,9]]
newInterval = [2,5]

def insert(intervals, newInterval):
    result = []
    i = 0
    # Add all intervals ending before newInterval starts
    while i < len(intervals) and intervals[i][1] < newInterval[0]:
        result.append(intervals[i])
        i += 1
    # Merge all overlapping intervals
    while i < len(intervals) 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 < len(intervals):
        result.append(intervals[i])
        i += 1
    return result

# Insert and merge intervals
result = insert(intervals, newInterval)
print(result)
This code inserts the interval [2,5] into the sorted list [[1,3],[6,9]] and merges overlapping intervals.
Execution Table
StepOperationCurrent IntervalNew IntervalActionIntervals List State
1Start-[2,5]Initial intervals and new interval[]
2Check overlap[1,3][2,5]Overlap detected, merge intervals[[1,5]]
3Move to next[6,9][1,5]No overlap, add current interval to result[[1,5],[6,9]]
4Insert remaining-[1,5]All intervals processed, insert merged interval if not added[[1,5],[6,9]]
5End--Return merged intervals list[[1,5],[6,9]]
💡 All intervals processed and merged where overlapping, final list returned.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
intervals[[1,3],[6,9]][[1,3],[6,9]][[1,3],[6,9]][[1,3],[6,9]]
newInterval[2,5][1,5][1,5][1,5]
result[][[1,5]][[1,5],[6,9]][[1,5],[6,9]]
i (index)0122
Key Moments - 3 Insights
Why do we merge intervals when they overlap instead of just inserting the new interval?
Because overlapping intervals represent continuous ranges that should be combined into one. As shown in Step 2 of the execution_table, merging [1,3] and [2,5] creates [1,5], avoiding duplicate or overlapping intervals.
What happens if the new interval does not overlap with any existing intervals?
The new interval is inserted in the correct sorted position without merging. This is implied in Step 3 and 4 where intervals without overlap are simply added to the result list.
Why do we continue checking intervals after merging the new interval?
Because the merged interval might overlap with subsequent intervals. The algorithm ensures all overlapping intervals are merged before final insertion, as seen in the flow from Step 2 to Step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 2, what is the new merged interval after merging?
A[1,3]
B[2,5]
C[1,5]
D[6,9]
💡 Hint
Check the 'New Interval' and 'Action' columns at Step 2 in the execution_table.
At which step does the algorithm add the interval [6,9] to the result list?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the 'Action' and 'Intervals List State' columns in the execution_table for when [6,9] appears in the result.
If the new interval was [10,12], how would the final intervals list look?
A[[1,3],[6,9]]
B[[1,3],[6,9],[10,12]]
C[[1,3],[6,12]]
D[[10,12],[1,3],[6,9]]
💡 Hint
Consider that [10,12] does not overlap and should be inserted at the end.
Concept Snapshot
Insert Interval into Sorted List:
- Input: sorted list of intervals and a new interval
- Traverse intervals to find overlap
- Merge overlapping intervals by updating start/end
- Insert merged/new interval in correct position
- Return updated sorted intervals list
Full Transcript
This visualization shows how to insert a new interval into a sorted list of intervals. We start with the original intervals and the new interval. We check each interval for overlap with the new interval. If they overlap, we merge them by updating the start and end of the new interval. If no overlap, we add the current interval to the result. After processing all intervals, we insert the merged new interval if not already added. The final list is sorted and merged without overlaps.