Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to sort the intervals by their start time.
DSA C++
std::sort(intervals.begin(), intervals.end(), [](const Interval &a, const Interval &b) {
return a.[1] < b.[1];
});Attempts:
3 left
💡 Hint
Common Mistakes
Sorting by end time instead of start time.
Using an attribute that does not exist.
✗ Incorrect
We sort intervals by their start time to process meetings in chronological order.
2fill in blank
mediumComplete the code to check if the current meeting overlaps with the earliest ending meeting.
DSA C++
if (intervals[i].start < minHeap.[1]()) { rooms++; } else { minHeap.pop(); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'front()' which is for queues, not priority queues.
Using 'peek()' which is not a C++ priority_queue method.
✗ Incorrect
The 'top' function returns the smallest end time in the min heap to check overlap.
3fill in blank
hardFix the error in the code to push the current meeting's end time into the min heap.
DSA C++
minHeap.[1](intervals[i].end);Attempts:
3 left
💡 Hint
Common Mistakes
Using 'insert' which is not a method of priority_queue.
Using 'enqueue' which is not a C++ priority_queue method.
✗ Incorrect
The correct method to add an element to a priority_queue in C++ is 'push'.
4fill in blank
hardFill both blanks to create a min heap that stores meeting end times.
DSA C++
std::priority_queue<int, std::vector<int>, [1]> minHeap; minHeap.push(intervals[0].[2]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'std::less' which creates a max heap.
Pushing the 'start' time instead of 'end' time.
✗ Incorrect
A min heap uses 'std::greater' as comparator, and we push the 'end' time of the interval.
5fill in blank
hardFill all three blanks to complete the loop that processes all meetings and counts minimum rooms.
DSA C++
for (int i = 1; i < intervals.size(); i++) { if (intervals[i].[1] < minHeap.[2]()) { rooms++; } else { minHeap.[3](); } minHeap.push(intervals[i].end); }
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with 'end' instead of 'start'.
Using 'pop' incorrectly or missing it.
✗ Incorrect
We compare the current meeting's start with the earliest end time (top), pop if no overlap, and push the current end.
