0
0
DSA Pythonprogramming~10 mins

Insert Interval into Sorted List in DSA Python - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to insert the new interval into the list.

DSA Python
def insert_interval(intervals, new_interval):
    result = []
    i = 0
    while i < len(intervals) and intervals[i][1] < [1][0]:
        result.append(intervals[i])
        i += 1
    return result
Drag options to blanks, or click blank then click option'
Anew_interval
Bintervals
Cresult
Di
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with intervals instead of new_interval
Using wrong index in comparison
2fill in blank
medium

Complete the code to merge overlapping intervals with the new interval.

DSA Python
while i < len(intervals) and intervals[i][0] <= [1][1]:
    new_interval[0] = min(new_interval[0], intervals[i][0])
    new_interval[1] = max(new_interval[1], intervals[i][1])
    i += 1
Drag options to blanks, or click blank then click option'
Aintervals[i]
Bnew_interval
Cresult
Di
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable for comparison
Not updating new_interval boundaries correctly
3fill in blank
hard

Fix the error in adding the merged new interval to the result list.

DSA Python
result.append([1])
Drag options to blanks, or click blank then click option'
Anew_interval
Bintervals[i]
Cintervals
Dresult
Attempts:
3 left
💡 Hint
Common Mistakes
Appending intervals[i] which may be out of range
Appending the whole intervals list
4fill in blank
hard

Fill both blanks to add remaining intervals after the new interval.

DSA Python
while [1] < len(intervals):
    result.[2](intervals[i])
    i += 1
Drag options to blanks, or click blank then click option'
Ai
Bappend
Cintervals
Dinsert
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable for index
Using insert instead of append
5fill in blank
hard

Fill all three blanks to complete the insert_interval function.

DSA Python
def insert_interval(intervals, new_interval):
    result = []
    i = 0
    while i < len(intervals) and intervals[i][1] < [1][0]:
        result.append(intervals[i])
        i += 1
    while i < len(intervals) and intervals[i][0] <= [2][1]:
        [3][0] = min([3][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
    return result
Drag options to blanks, or click blank then click option'
Anew_interval
Bintervals
Dresult
Attempts:
3 left
💡 Hint
Common Mistakes
Using intervals instead of new_interval in comparisons
Not updating new_interval boundaries correctly