Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
We sort intervals by their start time, which is at index 0 in each interval list.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking wrong index for start time.
Using '<' instead of '<=' misses touching intervals.
✗ Incorrect
We compare the start of the current interval (index 0) with the end of the last merged interval.
3fill in blank
hardFix 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'
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.
✗ Incorrect
We update the end time using the end of the current interval, which is at index 1.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting loop from 0 causes duplicate processing of first interval.
Appending wrong interval index to merged.
✗ Incorrect
We start merged with the first interval at index 0 and loop from index 1 to the end.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up start and end indices.
Looping from 0 instead of 1 causing duplicates.
✗ Incorrect
Sort by start index 0, start merged with intervals[0], loop from 1 to end.
