Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create an interval object with start and end properties.
DSA Typescript
const interval = { start: 1, end: [1] };Attempts:
3 left
💡 Hint
Common Mistakes
Using the variable name 'start' instead of a number for end.
Setting end to 0 which is less than start.
✗ Incorrect
The interval ends at 5, so the end property should be 5.
2fill in blank
mediumComplete the code to check if two intervals overlap.
DSA Typescript
function isOverlap(a: {start: number, end: number}, b: {start: number, end: number}): boolean {
return a.start <= b.end && b.start [1] a.end;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using > which would miss overlapping intervals.
Using == which only checks exact matches.
✗ Incorrect
Two intervals overlap if a.start <= b.end and b.start <= a.end.
3fill in blank
hardFix the error in the function that merges two overlapping intervals.
DSA Typescript
function mergeIntervals(a: {start: number, end: number}, b: {start: number, end: number}): {start: number, end: number} {
return { start: Math.min(a.start, b.start), end: Math.max(a.end, [1]) };
}Attempts:
3 left
💡 Hint
Common Mistakes
Using a.start or b.start instead of end values.
Using Math.min instead of Math.max for end.
✗ Incorrect
To merge intervals, take the maximum end value between a.end and b.end.
4fill in blank
hardFill both blanks to create a function that sorts intervals by their start time.
DSA Typescript
function sortIntervals(intervals: {start: number, end: number}[]): {start: number, end: number}[] {
return intervals.sort((a, b) => a.[1] - b.[2]);
}Attempts:
3 left
💡 Hint
Common Mistakes
Sorting by end which may not order intervals correctly.
Using properties that do not exist like length or value.
✗ Incorrect
Intervals should be sorted by their start property to process them in order.
5fill in blank
hardFill all three blanks to create a function that filters intervals longer than 5 units.
DSA Typescript
function filterLongIntervals(intervals: {start: number, end: number}[]): {start: number, end: number}[] {
return intervals.filter(interval => (interval.[1] - interval.[2]) [3] 5);
}Attempts:
3 left
💡 Hint
Common Mistakes
Subtracting start - end which gives negative length.
Using <= instead of > which filters wrong intervals.
✗ Incorrect
Interval length is end - start; filter intervals where length > 5.
