Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with intervals instead of new_interval
Using wrong index in comparison
✗ Incorrect
We compare the end of intervals[i] with the start of new_interval to find where to insert.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable for comparison
Not updating new_interval boundaries correctly
✗ Incorrect
We merge intervals overlapping with new_interval by updating new_interval boundaries.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Appending intervals[i] which may be out of range
Appending the whole intervals list
✗ Incorrect
After merging, we add the updated new_interval to the result list.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable for index
Using insert instead of append
✗ Incorrect
We continue from index i and append remaining intervals to result.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using intervals instead of new_interval in comparisons
Not updating new_interval boundaries correctly
✗ Incorrect
We use new_interval to compare and update boundaries during merging.