Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to sort intervals by their end time.
DSA C
int compare(const void *a, const void *b) {
int *intervalA = *(int **)a;
int *intervalB = *(int **)b;
return intervalA[[1]] - intervalB[[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 for end time.
✗ Incorrect
We sort intervals by their end time, which is at index 1 in each interval array.
2fill in blank
mediumComplete the code to initialize the count of non-overlapping intervals.
DSA C
int count = 1; int prev_end = intervals[0][[1]];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using start time instead of end time.
Initializing count to 0 instead of 1.
✗ Incorrect
We start with the first interval's end time at index 1 to compare with others.
3fill in blank
hardFix the error in the condition to check if intervals overlap.
DSA C
for (int i = 1; i < intervalsSize; i++) { if (intervals[i][0] >= [1]) { count++; prev_end = intervals[i][1]; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing start time with start time.
Using wrong variable in condition.
✗ Incorrect
We compare the current interval's start time with the previous interval's end time to check overlap.
4fill in blank
hardFill both blanks to calculate the minimum number of intervals to remove.
DSA C
int minRemoval = [1] - [2]; return minRemoval;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Subtracting wrong variables.
Returning count instead of minimum removals.
✗ Incorrect
Minimum removals = total intervals - count of non-overlapping intervals.
5fill in blank
hardFill all three blanks to complete the function for minimum removals.
DSA C
int eraseOverlapIntervals(int** intervals, int intervalsSize, int* intervalsColSize) {
qsort(intervals, intervalsSize, sizeof(int*), [1]);
int count = 1;
int prev_end = intervals[0][[2]];
for (int i = 1; i < intervalsSize; i++) {
if (intervals[i][0] >= [3]) {
count++;
prev_end = intervals[i][1];
}
}
return intervalsSize - count;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong function name in qsort.
Using wrong indices for start/end times.
Incorrect overlap condition.
✗ Incorrect
We use compare function for sorting, index 1 for end time, and prev_end to check overlap.
