Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to sort the intervals by their start time.
DSA Javascript
intervals.sort((a, b) => a[1]b);Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect properties like 'start' when intervals are arrays.
Not subtracting correctly causing wrong sort order.
✗ Incorrect
We sort intervals by their start time which is at index 0, so we subtract b[0] from a[0].
2fill in blank
mediumComplete the code to push the end time of the first meeting into the heap.
DSA Javascript
const heap = []; heap.push(intervals[0][1]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 0 which is the start time instead of end time.
Trying to access a property 'end' which does not exist.
✗ Incorrect
We push the end time of the first meeting which is at index 1 in the interval array.
3fill in blank
hardFix the error in the condition to check if the current meeting can reuse a room.
DSA Javascript
if (intervals[i][0] [1] heap[0]) { heap.shift(); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' or '<=' which reverses the logic.
Using '>' which misses the equal case.
✗ Incorrect
If the current meeting starts at or after the earliest ending meeting, we can reuse that room.
4fill in blank
hardFill both blanks to insert the current meeting's end time and keep the heap sorted.
DSA Javascript
heap.push(intervals[i][1]); heap.sort((a, b) => a [2] b);
Attempts:
3 left
💡 Hint
Common Mistakes
Pushing start time instead of end time.
Sorting with '+' which does not sort correctly.
✗ Incorrect
We push the end time at index 1 and sort the heap by ascending end times using subtraction.
5fill in blank
hardFill all three blanks to return the minimum number of rooms needed.
DSA Javascript
function minMeetingRooms(intervals) {
if (intervals.length === 0) return 0;
intervals.sort((a, b) => a[0] - b[0]);
const heap = [];
heap.push(intervals[0][1]);
for (let i = 1; i < intervals.length; i++) {
if (intervals[i][0] [2] heap[0]) {
heap.shift();
}
heap.push(intervals[i][3]);
heap.sort((a, b) => a - b);
}
return heap.length;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using start times instead of end times for heap operations.
Incorrect comparison operator in the if condition.
✗ Incorrect
We push end times at index 1, check if current start time is >= earliest end time, and push current end time again at index 1.
