Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to merge overlapping intervals.
DSA Javascript
function mergeIntervals(intervals) {
intervals.sort((a, b) => a[[1]] - b[[1]]);
const merged = [];
for (const interval of intervals) {
if (!merged.length || merged[merged.length - 1][1] < interval[0]) {
merged.push(interval);
} else {
merged[merged.length - 1][1] = Math.max(merged[merged.length - 1][1], interval[1]);
}
}
return merged;
}Attempts:
3 left
💡 Hint
Common Mistakes
Sorting by end time instead of start time.
Using incorrect index for start time.
✗ 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 two intervals overlap.
DSA Javascript
function isOverlap(interval1, interval2) {
return interval1[1] > interval2[[1]] && interval2[1] > interval1[0];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong index for start or end.
Using >= instead of > which may cause incorrect overlap detection.
✗ Incorrect
We check if the end of the first interval is after the start of the second interval (index 0).
3fill in blank
hardFix the error in the code to insert a new interval into a list of non-overlapping intervals.
DSA Javascript
function insertInterval(intervals, newInterval) {
const result = [];
let i = 0;
while (i < intervals.length && intervals[i][1] < newInterval[[1]]) {
result.push(intervals[i]);
i++;
}
while (i < intervals.length && intervals[i][0] <= newInterval[1]) {
newInterval[0] = Math.min(newInterval[0], intervals[i][0]);
newInterval[1] = Math.max(newInterval[1], intervals[i][1]);
i++;
}
result.push(newInterval);
while (i < intervals.length) {
result.push(intervals[i]);
i++;
}
return result;
}Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with wrong index of newInterval.
Mixing up start and end indices.
✗ Incorrect
We compare the end of current intervals with the start of newInterval, which is at index 0.
4fill in blank
hardFill both blanks to create a dictionary of intervals keyed by their start time and filtered by intervals longer than 2 units.
DSA Javascript
const filteredIntervals = intervals.reduce((acc, interval) => {
if (interval[[1]] - interval[[2]] > 2) {
acc[interval[0]] = interval;
}
return acc;
}, {});Attempts:
3 left
💡 Hint
Common Mistakes
Swapping start and end indices.
Using same index for both blanks.
✗ Incorrect
We subtract start (index 0) from end (index 1) to get interval length.
5fill in blank
hardFill all three blanks to create a map of interval lengths keyed by the interval's start time, only for intervals starting after 5.
DSA Javascript
const intervalLengths = intervals.reduce((map, interval) => {
if (interval[[1]] > 5) {
map[interval[[2]]] = interval[[3]] - interval[0];
}
return map;
}, {});Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong indices for start or end.
Mixing up keys and values in the map.
✗ Incorrect
We check start time at index 0, use it as key, and calculate length as end (index 1) minus start (index 0).
