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.Start [1] b.Start);Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' results in incorrect (descending) sorting order.
Using '>' causes incorrect sorting.
Using '==' causes no sorting.
✗ Incorrect
We use a.Start - b.Start which returns negative if a.Start is less than b.Start to sort ascending by start time.
2fill in blank
mediumComplete the code to check if the current meeting starts after the earliest ending meeting.
DSA C#
if (intervals[i].Start [1] minHeap.Peek())
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' or '<=' causes wrong logic for room reuse.
Using '>' excludes meetings starting exactly at end time.
✗ Incorrect
If the current meeting starts at or after the earliest ending meeting, we can reuse that room.
3fill in blank
hardFix the error in removing the earliest ending meeting from the min heap.
DSA C#
minHeap.[1]();Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Pop', 'Poll', or 'Remove' causes error as they are not methods in C# PriorityQueue.
✗ Incorrect
In C#, PriorityQueue uses 'Dequeue' to remove the smallest element.
4fill in blank
hardFill both blanks to add the current meeting's end time to the min heap and update the minimum rooms required.
DSA C#
minHeap.[1](intervals[i].End, intervals[i].End); minRooms = Math.[2](minRooms, minHeap.Count);
Attempts:
3 left
💡 Hint
Common Mistakes
Using Min instead of Max causes incorrect room count.
Using Add instead of Enqueue causes error.
✗ Incorrect
We add the current meeting's end time to the min heap using Enqueue. Then update minRooms with the maximum of current minRooms and minHeap count.
5fill in blank
hardFill all three blanks to complete the method that returns the minimum number of meeting rooms required.
DSA C#
public int MinMeetingRooms(Interval[] intervals) {
if (intervals == null || intervals.Length == 0) return 0;
Array.Sort(intervals, (a, b) => a.Start [1] b.Start);
var minHeap = new PriorityQueue<int, int>();
minHeap.[2](intervals[0].End, intervals[0].End);
int minRooms = 1;
for (int i = 1; i < intervals.Length; i++) {
if (intervals[i].Start [3] minHeap.Peek()) {
minHeap.Dequeue();
}
minHeap.Enqueue(intervals[i].End, intervals[i].End);
minRooms = Math.Max(minRooms, minHeap.Count);
}
return minRooms;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' or '>' in sorting causes wrong order.
Using 'Dequeue' instead of 'Enqueue' to add element causes error.
Using '<=' instead of '>=' in condition causes wrong room reuse logic.
✗ Incorrect
Sort intervals by start time using '-'. Add first meeting end time with Enqueue. Check if current meeting starts at or after earliest end time with '>='.
