Mastering LeetCode's "Insert Interval": A Comprehensive Guide

Unraveling the intricacies of the "Insert Interval" challenge on LeetCode with multi-language solutions for a clear edge in technical interviews.

Β·

4 min read

Mastering LeetCode's "Insert Interval": A Comprehensive Guide

Welcome to a deep dive into solving the "Insert Interval" (LeetCode 57) problem, a common yet intriguing challenge often encountered in coding interviews and on platforms like LeetCode.

This problem not only tests your understanding of array manipulation but also your ability to handle edge cases gracefully. Let's embark on a journey to unpack, solve, and understand this problem from the ground up.

Introduction to the Problem

At its core, the "Insert Interval" problem involves integrating a new interval into a list of existing, non-overlapping intervals sorted by their start times. The crux of the challenge lies in ensuring that the resultant list remains sorted and free of overlaps, necessitating the merger of intervals when overlaps occur.

You are given an array of non-overlapping intervals intervalswhere intervals[i] = [start<sub>i</sub>, end<sub>i</sub>] represent the start and the end of the i<sup>th</sup> interval and intervals is sorted in ascending order by start<sub>i</sub>. You are also given an interval newInterval = [start, end] that represents the start and end of another interval.

Insert newInterval into intervals such that intervals is still sorted in ascending order by start<sub>i</sub> and intervals still does not have any overlapping intervals (merge overlapping intervals if necessary).

Return intervals after the insertion.

Note that you don't need to modify intervals in-place. You can make a new array and return it.

Example 1:

Input: intervals = [[1,3],[6,9]], newInterval = [2,5]
Output: [[1,5],[6,9]]

Example 2:

Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
Output: [[1,2],[3,10],[12,16]]
Explanation: Because the new interval [4,8] overlaps with [3,5],[6,7],[8,10].

Consideration of Different Approaches

There are primarily three cases to consider when inserting a new interval:

  1. The new interval does not overlap and lies to the left of the current interval.

  2. The new interval does not overlap and lies to the right of the current interval.

  3. The new interval overlaps with the current interval, requiring a merge.

A naive approach might involve checking each interval individually and deciding where to place the new interval or how to merge intervals. However, this can be inefficient, especially with a large number of intervals.

A more efficient approach involves iterating through the list of intervals while maintaining a result list. We compare the new interval with each existing interval, deciding whether to add the existing interval to the result list, merge intervals, or insert the new interval before moving on.

Description of the Solution

The optimal solution iterates through the intervals with three main outcomes for each interval in relation to the new interval: insertion (when the current interval lies entirely to the left or right of the new interval) and merging (when there is an overlap).

  • Time Complexity: The solution runs in O(n) time, where n is the number of intervals, since it involves a single pass through the list of intervals.

  • Space Complexity: O(n) for the result list, which is the worst-case space requirement when no intervals are merged.

The Solution in Python

def insert(intervals, newInterval):
    result = []
    for interval in intervals:
        if interval[1] < newInterval[0]:  # New interval is right of the current interval
            result.append(interval)
        elif interval[0] > newInterval[1]:  # New interval is left of the current interval
            result.append(newInterval)
            newInterval = interval  # Update newInterval to the current one, as it's not inserted yet
        else:  # Overlapping intervals, merge them
            newInterval[0] = min(interval[0], newInterval[0])  # Take the min start time
            newInterval[1] = max(newInterval[1], interval[1])  # Take the max end time
    result.append(newInterval)  # Add the last interval, which might be merged or the original new interval
    return result

The Solution in TypeScript

function insert(intervals: number[][], newInterval: number[]): number[][] {
    let result: number[][] = [];
    for (let interval of intervals) {
        if (interval[1] < newInterval[0]) {
            result.push(interval);
        } else if (interval[0] > newInterval[1]) {
            result.push(newInterval);
            newInterval = interval;
        } else {
            newInterval = [
                Math.min(interval[0], newInterval[0]), 
                Math.max(newInterval[1], interval[1])
            ];
        }
    }
    result.push(newInterval);
    return result;
}

The Solution in Java

public int[][] insert(int[][] intervals, int[] newInterval) {
    List<int[]> result = new ArrayList<>();
    for (int[] interval : intervals) {
        if (interval[1] < newInterval[0]) {
            result.add(interval);
        } else if (interval[0] > newInterval[1]) {
            result.add(newInterval);
            newInterval = interval;
        } else {
            newInterval[0] = Math.min(interval[0], newInterval[0]);
            newInterval[1] = Math.max(newInterval[1], interval[1]);
        }
    }
    result.add(newInterval);
    return result.toArray(new int[result.size()][]);
}

Conclusion

Solving the "Insert Interval" problem efficiently is crucial for showcasing your problem-solving skills in software engineering interviews. By understanding and implementing the solutions in Python, TypeScript, and Java, you demonstrate not only your coding proficiency across multiple languages but also a deep comprehension of algorithmic challenges.

Remember, practicing such problems enhances your ability to tackle array manipulation and interval merging tasks, key skills in the arsenal of any aspiring software engineer.

Did you find this article valuable?

Support Sean Coughlin by becoming a sponsor. Any amount is appreciated!

Β