Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to sort the intervals by their start time.
DSA C#
Array.Sort(intervals, (a, b) => a[[1]].CompareTo(b[0]));
Attempts:
3 left
💡 Hint
Common Mistakes
Using the end time index (1) instead of start time (0).
Using Length or invalid indices.
✗ Incorrect
We sort intervals by their start time, which is at index 0 in each interval array.
2fill in blank
mediumComplete the code to check if the current interval overlaps with the last merged interval.
DSA C#
if (intervals[i][0] [1] merged[merged.Count - 1][1])
Attempts:
3 left
💡 Hint
Common Mistakes
Using < or > which misses touching intervals.
Using >= which reverses the logic.
✗ Incorrect
If the current interval's start is less than or equal to the last merged interval's end, they overlap.
3fill in blank
hardFix the error in updating the end of the last merged interval to the maximum end time.
DSA C#
merged[merged.Count - 1][1] = Math.[1](merged[merged.Count - 1][1], intervals[i][1]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using Min which shrinks the interval incorrectly.
Using Abs or Round which are unrelated.
✗ Incorrect
We update the end to the maximum of the two ends to merge intervals correctly.
4fill in blank
hardFill both blanks to add a new interval to merged list and continue the loop.
DSA C#
merged.[1](intervals[i]); i[2];
Attempts:
3 left
💡 Hint
Common Mistakes
Using Append which is not a List method in C#.
Using -- which decreases the index incorrectly.
✗ Incorrect
Use Add to append to the list and ++ to move to the next index.
5fill in blank
hardFill the blanks to convert the merged list to an array and return it.
DSA C#
return merged.[1](); // merged is List<int[]> // Convert to [2]
Attempts:
3 left
💡 Hint
Common Mistakes
Using ToList() which returns a list, not array.
Returning wrong type like int[] instead of int[][].
✗ Incorrect
Use ToArray() to convert List to array, and specify return type int[][] for intervals.
