0
0
DSA Pythonprogramming~10 mins

Non Overlapping Intervals Minimum Removal 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 intervals by their end time.

DSA Python
intervals.sort(key=lambda x: x[1])
Drag options to blanks, or click blank then click option'
A[1]
B[
C[0]
D[2]
Attempts:
3 left
💡 Hint
Common Mistakes
Sorting by start time instead of end time.
Using wrong index like 0 or 2.
2fill in blank
medium

Complete the code to check if current interval overlaps with previous.

DSA Python
if intervals[i][0] < intervals[[1]][1]:
Drag options to blanks, or click blank then click option'
A0
Bi+1
Ci-1
Dlen(intervals)-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using i+1 which is next interval, not previous.
Using 0 or last index incorrectly.
3fill in blank
hard

Fix the error in updating the end time after removing an interval.

DSA Python
if intervals[i][1] < intervals[prev][[1]]:
    prev = i
Drag options to blanks, or click blank then click option'
A1
B0
C2
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 0 which is start time.
Using invalid index like 2 or -1.
4fill in blank
hard

Fill both blanks to complete the loop that counts removals.

DSA Python
removals = 0
prev = 0
for i in range(1, [1]):
    if intervals[i][0] < intervals[prev][1]:
        removals += 1
        if intervals[i][[2]] < intervals[prev][1]:
            prev = i
    else:
        prev = i
Drag options to blanks, or click blank then click option'
Alen(intervals)
B0
C1
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or 2 for loop range end.
Comparing start time instead of end time.
5fill in blank
hard

Fill all three blanks to complete the function that returns minimum removals.

DSA Python
def eraseOverlapIntervals(intervals):
    if not intervals:
        return 0
    intervals.sort(key=lambda x: x[1])
    removals = 0
    prev = 0
    for i in range(1, [2]):
        if intervals[i][0] < intervals[prev][1]:
            removals += 1
            if intervals[i][[3]] < intervals[prev][1]:
                prev = i
        else:
            prev = i
    return removals
Drag options to blanks, or click blank then click option'
A[1]
Blen(intervals)
C1
D[0]
Attempts:
3 left
💡 Hint
Common Mistakes
Sorting by start time instead of end time.
Looping with wrong range.
Comparing wrong indices.