Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to sort intervals by their start time.
DSA Typescript
intervals.sort((a, b) => [1]);Attempts:
3 left
💡 Hint
Common Mistakes
Using 'a - b' instead of 'a.start - b.start'.
Forgetting to access the start property.
✗ Incorrect
We sort intervals by their start property using a.start - b.start.
2fill in blank
mediumComplete the code to check if current interval overlaps with last merged interval.
DSA Typescript
if (intervals[i].start [1] merged[merged.length - 1].end) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of <= causes missing overlaps.
Using < excludes touching intervals.
✗ Incorrect
If current start is less than or equal to last merged end, intervals overlap.
3fill in blank
hardFix the error in updating the end of the last merged interval.
DSA Typescript
merged[merged.length - 1].end = Math.[1](merged[merged.length - 1].end, intervals[i].end);
Attempts:
3 left
💡 Hint
Common Mistakes
Using Math.min causes incorrect merging.
Using Math.abs or Math.round is irrelevant here.
✗ Incorrect
We take the maximum end to merge overlapping intervals correctly.
4fill in blank
hardFill both blanks to add a non-overlapping interval to merged list.
DSA Typescript
else { merged.[1](intervals[[2]]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop removes last element instead of adding.
Using j instead of i.
✗ Incorrect
We push the current interval at index i to merged when no overlap.
5fill in blank
hardFill all three blanks to complete the mergeIntervals function.
DSA Typescript
function mergeIntervals(intervals: {start: number, end: number}[]): {start: number, end: number}[] {
if (intervals.length === 0) return [];
intervals.sort((a, b) => a.[1] - b.[2]);
const merged = [intervals[0]];
for (let [3] = 1; [3] < intervals.length; [3]++) {
if (intervals[[3]].start <= merged[merged.length - 1].end) {
merged[merged.length - 1].end = Math.max(merged[merged.length - 1].end, intervals[[3]].end);
} else {
merged.push(intervals[[3]]);
}
}
return merged;
}Attempts:
3 left
💡 Hint
Common Mistakes
Sorting by end instead of start.
Using wrong loop variable or missing loop increment.
✗ Incorrect
Sort by start (A and A), loop with i (C), and access start property correctly.
