Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to sort intervals by their end time.
DSA Java
Arrays.sort(intervals, (a, b) -> a[[1]] - b[[1]]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using start time index instead of end time index for sorting.
Mixing indices between 'a' and 'b' in comparator.
✗ Incorrect
We sort intervals by their end time, which is at index 1. The blank is for the first interval's end index, so it should be 1.
2fill in blank
mediumComplete the code to count intervals to remove for no overlap.
DSA Java
if (intervals[i][0] < intervals[[1]][1]) count++; else last = i;
Attempts:
3 left
💡 Hint
Common Mistakes
Using i-1 or i+1 instead of last index.
Comparing wrong interval indices.
✗ Incorrect
We compare the current interval's start with the last non-overlapping interval's end. 'last' stores the index of the last interval kept.
3fill in blank
hardFix the error in the loop condition to iterate over all intervals.
DSA Java
for (int i = 1; i [1] intervals.length; i++) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causes index out of bounds.
Using '>' or '>=' causes loop not to run.
✗ Incorrect
The loop should run while i is less than intervals.length to avoid ArrayIndexOutOfBoundsException.
4fill in blank
hardFill both blanks to complete the method signature and return statement.
DSA Java
public int [1](int[][] intervals) { if (intervals.length == 0) return [2]; // method body }
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method name.
Returning -1 instead of 0 for empty input.
✗ Incorrect
The method name is 'eraseOverlapIntervals' and returns 0 if no intervals exist.
5fill in blank
hardFill all three blanks to complete the main logic for counting removals.
DSA Java
Arrays.sort(intervals, (a, b) -> a[[1]] - b[[2]]); int count = 0, last = 0; for (int i = 1; i [3] intervals.length; i++) { if (intervals[i][0] < intervals[last][1]) count++; else last = i; } return count;
Attempts:
3 left
💡 Hint
Common Mistakes
Sorting by start time instead of end time.
Using wrong loop condition causing errors.
✗ Incorrect
Sort by end time (index 1), loop while i < intervals.length.
