Bird
0
0
DSA Pythonprogramming~10 mins

Merge Overlapping Intervals 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 sort the intervals by their start time.

DSA Python
intervals.sort(key=lambda x: x[[1]])
Drag options to blanks, or click blank then click option'
A1
B0
C2
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 1 instead of 0 causes sorting by end time, which is incorrect.
Using negative index like -1 sorts by end but is less clear.
2fill in blank
medium

Complete the code to check if the current interval overlaps with the last merged interval.

DSA Python
if intervals[i][[1]] <= merged[-1][1]:
Drag options to blanks, or click blank then click option'
A1
B-1
C2
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Checking wrong index for start time.
Using '<' instead of '<=' misses touching intervals.
3fill in blank
hard

Fix the error in updating the end time of the last merged interval.

DSA Python
merged[-1][1] = max(merged[-1][1], intervals[i][[1]])
Drag options to blanks, or click blank then click option'
A1
B0
C2
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 0 (start) instead of 1 (end) causes wrong merging.
Using negative index -1 can be confusing here.
4fill in blank
hard

Fill both blanks to append the first interval and loop through the rest.

DSA Python
merged = [intervals[[1]]]
for i in range([2], len(intervals)):
    # merging logic here
Drag options to blanks, or click blank then click option'
A0
B1
C2
Dlen(intervals)
Attempts:
3 left
💡 Hint
Common Mistakes
Starting loop from 0 causes duplicate processing of first interval.
Appending wrong interval index to merged.
5fill in blank
hard

Fill all three blanks to complete the merge intervals function.

DSA Python
def merge(intervals):
    if not intervals:
        return []
    intervals.sort(key=lambda x: x[[1]])
    merged = [intervals[[2]]]
    for i in range([3], len(intervals)):
        if intervals[i][0] <= merged[-1][1]:
            merged[-1][1] = max(merged[-1][1], intervals[i][1])
        else:
            merged.append(intervals[i])
    return merged
Drag options to blanks, or click blank then click option'
A0
C1
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up start and end indices.
Looping from 0 instead of 1 causing duplicates.