Why Intervals Are a Common Problem Pattern in DSA Typescript - Performance Analysis
Intervals problems often involve checking or merging ranges, which requires comparing many pairs.
We want to understand how the time needed grows as the number of intervals increases.
Analyze the time complexity of the following code snippet.
function canAttendMeetings(intervals: number[][]): boolean {
for (let i = 0; i < intervals.length; i++) {
for (let j = i + 1; j < intervals.length; j++) {
if (intervals[i][0] < intervals[j][1] && intervals[j][0] < intervals[i][1]) {
return false;
}
}
}
return true;
}
This code checks if any intervals overlap by comparing each pair.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Nested loops comparing pairs of intervals.
- How many times: Outer loop runs n times; inner loop runs up to n-1 times, so roughly n x n = n² times.
As the number of intervals grows, the number of comparisons grows much faster.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 100 comparisons |
| 100 | About 10,000 comparisons |
| 1000 | About 1,000,000 comparisons |
Pattern observation: Doubling the intervals roughly quadruples the work, showing a square growth.
Time Complexity: O(n²)
This means the time needed grows quickly as the number of intervals increases, because every interval is compared with many others.
[X] Wrong: "Checking intervals is always fast because we just look at each interval once."
[OK] Correct: Many interval problems require comparing pairs, which means the work grows much faster than just looking once.
Understanding how interval problems scale helps you explain your approach clearly and choose efficient methods during interviews.
"What if we sort the intervals first and then check only adjacent pairs? How would the time complexity change?"
Practice
Solution
Step 1: Understand the nature of interval problems
Interval problems often involve ranges of values, like time slots or numeric ranges, which are common in real-world tasks such as scheduling.Step 2: Identify why intervals are useful
Intervals allow efficient merging and checking overlaps, which are key in many applications like calendar events or resource allocation.Final Answer:
Because they help solve scheduling and merging tasks efficiently -> Option AQuick Check:
Intervals = scheduling and merging [OK]
- Assuming intervals only involve sorting
- Confusing intervals with graph problems
- Thinking recursion is always needed
Solution
Step 1: Recall TypeScript tuple syntax
Tuples in TypeScript are declared with square brackets and types inside, like [number, number].Step 2: Check each option
let interval: [number, number] = [5, 10]; correctly uses tuple syntax. let interval = (5, 10); uses parentheses which is invalid. let interval: number[] = (5, 10); uses array type but assigns parentheses. let interval: tuple = [5, 10]; uses 'tuple' which is not a TypeScript type.Final Answer:
let interval: [number, number] = [5, 10]; -> Option DQuick Check:
Tuple syntax = [number, number] [OK]
- Using parentheses instead of square brackets
- Using 'tuple' as a type keyword
- Confusing arrays with tuples
function mergeIntervals(intervals: [number, number][]): [number, number][] {
intervals.sort((a, b) => a[0] - b[0]);
const merged: [number, number][] = [];
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;
}
console.log(mergeIntervals([[1,3],[2,6],[8,10],[15,18]]));Solution
Step 1: Sort intervals by start time
The intervals become [[1,3],[2,6],[8,10],[15,18]].Step 2: Merge overlapping intervals
First interval [1,3] is added. Next [2,6] overlaps with [1,3], so merge to [1,6]. Then [8,10] and [15,18] do not overlap, so added as is.Final Answer:
[[1,6],[8,10],[15,18]] -> Option AQuick Check:
Merged intervals = [[1,6],[8,10],[15,18]] [OK]
- Not sorting intervals before merging
- Overwriting intervals incorrectly
- Missing to merge overlapping intervals
function isOverlap(a: [number, number], b: [number, number]): boolean {
return a[1] > b[0] && b[1] > a[0];
}Solution
Step 1: Understand interval overlap condition
Two intervals overlap if they share any point, including touching at edges.Step 2: Check the comparison operators
Using > excludes intervals that just touch (like [1,2] and [2,3]). Using >= includes touching intervals as overlapping.Final Answer:
The comparison should use >= instead of > to include touching intervals -> Option CQuick Check:
Use >= for touching intervals overlap [OK]
- Using > excludes touching intervals
- Returning wrong data type
- Wrong interval data types
Choose the best approach.
Solution
Step 1: Understand the problem of meeting rooms
We need to find how many meetings overlap at the same time to know rooms needed.Step 2: Use sorting and a min-heap
Sort intervals by start time. Use a min-heap to track earliest ending meeting. If a meeting starts after the earliest ends, reuse room; else add a new room.Step 3: Count the maximum size of the heap
The heap size at any time is the number of rooms needed.Final Answer:
Sort intervals by start time, use a min-heap to track end times, count rooms needed -> Option BQuick Check:
Min rooms = max overlapping intervals [OK]
- Merging intervals loses overlap info
- Using recursion causes inefficiency
- Simple division ignores overlaps
Ready to prove it?
Pass the test and this pattern moves unknown
