Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Sorting by start time instead of end time.
Using wrong index like 0 or 2.
✗ Incorrect
We sort intervals by their end time, which is at index 1 in each interval list.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using i+1 which is next interval, not previous.
Using 0 or last index incorrectly.
✗ Incorrect
We compare the start of current interval with end of previous interval at i-1.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 0 which is start time.
Using invalid index like 2 or -1.
✗ Incorrect
We compare end times at index 1, so update prev if current end is smaller.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or 2 for loop range end.
Comparing start time instead of end time.
✗ Incorrect
Loop runs from 1 to length of intervals; compare end time at index 1.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Sorting by start time instead of end time.
Looping with wrong range.
Comparing wrong indices.
✗ Incorrect
Sort by end time [1], loop till length, compare end time at index 1.