Insert Interval into Sorted List in DSA C - Time & Space Complexity
We want to know how the time to insert a new interval into a sorted list changes as the list grows.
Specifically, how many steps does it take to find the right place and merge intervals if needed?
Analyze the time complexity of the following code snippet.
void insertInterval(int intervals[][2], int* size, int newInterval[2]) {
int i = 0, pos = 0;
while (i < *size && intervals[i][1] < newInterval[0]) i++;
pos = i;
while (i < *size && intervals[i][0] <= newInterval[1]) {
newInterval[0] = (intervals[i][0] < newInterval[0]) ? intervals[i][0] : newInterval[0];
newInterval[1] = (intervals[i][1] > newInterval[1]) ? intervals[i][1] : newInterval[1];
i++;
}
// Shift intervals and insert merged newInterval at pos
// ... (shifting code omitted for brevity)
}
This code finds where to insert a new interval in a sorted list and merges overlapping intervals.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Two while loops scanning parts of the intervals array.
- How many times: Each loop runs at most n times combined, where n is the number of intervals.
As the number of intervals grows, the loops scan through the list to find overlaps and insertion point.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 steps scanning intervals |
| 100 | Up to 100 steps scanning intervals |
| 1000 | Up to 1000 steps scanning intervals |
Pattern observation: The number of steps grows roughly in direct proportion to the number of intervals.
Time Complexity: O(n)
This means the time to insert grows linearly with the number of intervals in the list.
[X] Wrong: "Since the list is sorted, insertion can be done in constant time."
[OK] Correct: Even though the list is sorted, we must scan to find the correct position and merge overlaps, which takes time proportional to the list size.
Understanding how to efficiently insert and merge intervals is a common skill that shows your ability to work with sorted data and handle edge cases.
"What if the intervals were stored in a balanced tree instead of an array? How would the time complexity change?"
Practice
Solution
Step 1: Understand the problem context
The list contains sorted, non-overlapping intervals. Inserting a new interval may cause overlaps.Step 2: Determine the correct insertion approach
We must merge any overlapping intervals with the new one and keep the list sorted to maintain the property.Final Answer:
Merge overlapping intervals and keep the list sorted -> Option DQuick Check:
Insert interval = merge + sort [OK]
- Inserting without merging causes overlaps
- Appending at the end breaks sorting
- Removing intervals unnecessarily
Solution
Step 1: Understand C struct typedef syntax
To define a struct and typedef it simultaneously, use: typedef struct Name { ... } Name;Step 2: Match the correct syntax
typedef struct Interval { int start; int end; } Interval; uses typedef struct Interval { int start; int end; } Interval; which is correct.Final Answer:
typedef struct Interval { int start; int end; } Interval; -> Option AQuick Check:
typedef struct Name { ... } Name; = typedef struct Interval { int start; int end; } Interval; [OK]
- Missing typedef keyword
- Incorrect struct declaration without name
- Declaring variable instead of type
Solution
Step 1: Identify overlapping intervals
The new interval [2,5] overlaps with [1,3] because 2 ≤ 3.Step 2: Merge overlapping intervals
Merge [1,3] and [2,5] into [1,5]. The interval [6,9] remains unchanged.Final Answer:
[[1,5], [6,9]] -> Option AQuick Check:
Merge [1,3] & [2,5] = [1,5] [OK]
- Not merging overlapping intervals
- Inserting new interval without sorting
- Appending new interval at wrong position
void insertInterval(Interval* intervals, int* size, Interval newInterval) {
int i = 0;
while (i < *size && intervals[i].end < newInterval.start) {
i++;
}
intervals[i] = newInterval; // Bug here
(*size)++;
}Solution
Step 1: Analyze insertion logic
The code finds position i but directly overwrites intervals[i] without moving later intervals.Step 2: Identify the error impact
Overwriting causes loss of data and corrupts the list. Proper insertion requires shifting elements right.Final Answer:
Overwrites an existing interval without shifting elements -> Option CQuick Check:
Overwrite without shift = data loss [OK]
- Ignoring element shifting
- Not handling overlaps
- Incorrect loop conditions
Solution
Step 1: Find intervals overlapping with [4,8]
Intervals [3,5], [6,7], and [8,10] overlap with [4,8].Step 2: Merge all overlapping intervals
Merge [3,5], [6,7], [8,10], and [4,8] into [3,10].Step 3: Construct final list
Keep [1,2] and [12,16] unchanged. Final list: [[1,2], [3,10], [12,16]].Final Answer:
[[1,2], [3,10], [12,16]] -> Option BQuick Check:
Merge overlapping intervals = [3,10] [OK]
- Not merging all overlapping intervals
- Inserting without merging
- Removing intervals incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
