Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to sort intervals by their end time.
DSA Go
sort.Slice(intervals, func(i, j int) bool {
return intervals[i].End [1] intervals[j].End
})Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' causes sorting in descending order, which breaks the algorithm.
Using '==' or '!=' does not sort properly.
✗ Incorrect
We sort intervals by their end time in ascending order to efficiently find non-overlapping intervals.
2fill in blank
mediumComplete the code to skip overlapping intervals by checking start time.
DSA Go
if intervals[i].Start [1] end { count++ } else { end = intervals[i].End }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causes wrong overlap detection.
Using '<' or '>' reverses the logic.
✗ Incorrect
If the current interval starts before the last selected interval ends, it overlaps and should be removed.
3fill in blank
hardFix the error in updating the end time after selecting a non-overlapping interval.
DSA Go
end = intervals[[1]].EndAttempts:
3 left
💡 Hint
Common Mistakes
Using i - 1 causes index errors or wrong interval end time.
Using 0 or last index does not update correctly.
✗ Incorrect
We update end to the end time of the current interval at index i after confirming no overlap.
4fill in blank
hardFill both blanks to initialize variables for counting removals and tracking end time.
DSA Go
count, end := 0, intervals[[1]].End for i := [2]; i < len(intervals); i++ { // loop body }
Attempts:
3 left
💡 Hint
Common Mistakes
Starting loop at 0 causes comparing the first interval with itself.
Setting end to intervals[1].End skips the first interval.
✗ Incorrect
We start count at 0 and set end to the end time of the first interval at index 0. The loop starts from index 1.
5fill in blank
hardFill all three blanks to complete the function that returns minimum removals for non-overlapping intervals.
DSA Go
func eraseOverlapIntervals(intervals []Interval) int {
if len(intervals) == 0 {
return 0
}
sort.Slice(intervals, func(i, j int) bool {
return intervals[i].[1] < intervals[j].[2]
})
count, end := 0, intervals[0].End
for i := 1; i < len(intervals); i++ {
if intervals[i].Start < end {
count++
} else {
end = intervals[i].[3]
}
}
return count
}Attempts:
3 left
💡 Hint
Common Mistakes
Sorting by Start time breaks the greedy approach.
Updating end with Start time causes wrong overlap checks.
✗ Incorrect
We sort intervals by their End time, so blanks 1 and 2 are 'End'. When updating end, we use the current interval's End time.
