Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to sort intervals by their end time.
DSA C#
Array.Sort(intervals, (a, b) => a[[1]].CompareTo(b[[1]]));
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong index for sorting (start instead of end).
Confusing the order of a and b in CompareTo.
✗ Incorrect
We sort intervals by their end time, which is at index 1. The code compares a[{{BLANK_1}}] with b[{{BLANK_1}}], so {{BLANK_1}} should be 1.
2fill in blank
mediumComplete the code to initialize the count of removals to zero.
DSA C#
int removals = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Starting removals at 1 instead of 0.
Initializing removals with the length of intervals.
✗ Incorrect
We start with zero removals because initially we haven't removed any intervals.
3fill in blank
hardFix the error in the if condition to detect overlapping intervals.
DSA C#
if (intervals[i][[1]] < intervals[prev][1])
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing end time with end time.
Using wrong indices for start or end.
✗ Incorrect
To check if the current interval starts before the previous interval ends, compare the start time of current (index 0) with end time of previous (index 1).
4fill in blank
hardFill both blanks to update the previous interval index and increment removals when overlap is found.
DSA C#
if (intervals[i][1] < intervals[prev][1]) prev = [1]; removals[2];
Attempts:
3 left
💡 Hint
Common Mistakes
Using += without a number.
Updating prev incorrectly.
✗ Incorrect
When current interval ends earlier, update prev to i. Then increment removals by 1 using ++.
5fill in blank
hardFill all three blanks to complete the loop that checks intervals and returns the removal count.
DSA C#
for (int [1] = 1; [2] < intervals.Length; [3]++) { if (intervals[i][0] < intervals[prev][1]) { if (intervals[i][1] < intervals[prev][1]) prev = i; removals++; } else { prev = i; } } return removals;
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in loop parts.
Starting loop from 0 instead of 1.
✗ Incorrect
The loop variable is i, starting from 1, running while i < intervals.Length, incrementing i each time.
