Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
HAPPY CASE
Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Explanation: Since intervals [1,3] and [2,6] overlap, merge them into [1,6].
Input: intervals = [[1,4],[4,5]]
Output: [[1,5]]
Explanation: Intervals [1,4] and [4,5] are considered overlapping.
EDGE CASE
Input: intervals = [[1,5]]
Output: [[1,5]]
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For Array/Strings, common solution patterns include:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: We will match the previous high against each low. If previous high is higher than low, then extend the sliding window and merge, otherwise close the window.
1. Go through all intervals in sorted order
2. Store first interval into results to start sliding window
3. Check high in previous sliding window against current low
a. If previous high is higher than low, then extend the sliding window and merge
b. Otherwise close the window
4. Return the results
⚠️ Common Mistakes
Implement the code to solve the algorithm.
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
result = []
# Go through all intervals in sorted order
for low, high in sorted(intervals):
# Store first interval into results to start sliding window
if not result:
result.append([low, high])
# Check high in previous sliding window against current low
previousHigh = result[-1][1]
if previousHigh >= low:
# If previous high is higher than low, then extend the sliding window and merge
result[-1][1] = max(previousHigh, high)
else:
# Otherwise close the window
result.append([low,high])
# Return the results
return result
class Solution {
public int[][] merge(int[][] intervals) {
if(intervals == null || intervals.length == 0)
return intervals;
// Go through all intervals in sorted order
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
// if end of previous interval is more than the start of current interval then there is a overlap
// Check high in previous sliding window against current low
LinkedList<int[]> mergedIntervals = new LinkedList<>();
for(int[] curr : intervals) {
// if list empty or no overlap simply add current interval close the window
if(mergedIntervals.isEmpty() || mergedIntervals.getLast()[1] < curr[0])
mergedIntervals.add(curr);
// If previous high is higher than low, then extend the sliding window and merge
else
mergedIntervals.getLast()[1] = Math.max(mergedIntervals.getLast()[1], curr[1]);
}
// Return the results
return mergedIntervals.toArray(new int[0][]);
}
}
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N represents the number of intervals.
O(NlogN)
, we need to sort the intervals before using the sliding window technique.O(1)
, not including the resulting output array.