Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to sort intervals by their start time.
DSA C
int compare(const void *a, const void *b) {
int *interval1 = (int *)a;
int *interval2 = (int *)b;
return interval1[[1]] - interval2[0];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the end time (index 1) instead of start time.
Using an invalid index like 2 or -1.
✗ Incorrect
We compare intervals by their start time, which is at index 0.
2fill in blank
mediumComplete the code to check if two intervals overlap.
DSA C
if (intervals[i][0] <= merged[mergedIndex][[1]]) { // Overlap detected }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing start with start instead of end.
Using wrong indices.
✗ Incorrect
We check if the current interval's start is less or equal to the last merged interval's end (index 1).
3fill in blank
hardFix the error in updating the end time of the merged interval.
DSA C
merged[mergedIndex][1] = (intervals[i][1] > merged[mergedIndex][[1]]) ? intervals[i][1] : merged[mergedIndex][1];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using start time index 0 instead of end time index 1.
Using invalid indices.
✗ Incorrect
We compare the current interval's end (index 1) with the merged interval's end (index 1).
4fill in blank
hardFill both blanks to correctly copy the current interval to merged when no overlap.
DSA C
mergedIndex++; merged[mergedIndex][[1]] = intervals[i][[2]];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using end time index 1 instead of start time 0.
Mixing up indices for source and destination.
✗ Incorrect
We copy the start time (index 0) from intervals[i] to merged[mergedIndex].
5fill in blank
hardFill all three blanks to copy both start and end times when no overlap.
DSA C
merged[mergedIndex][[1]] = intervals[i][[2]]; merged[mergedIndex][[3]] = intervals[i][1];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing start and end indices.
Using wrong indices for merged or intervals.
✗ Incorrect
Copy the start time (index 0) from intervals[i][0] to merged[mergedIndex][0] for blanks 1 and 2, and end time (index 1) for blank 3.
