avatarSantal Tech

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2382

Abstract

lass="hljs-number">1</span>:</pre></div><div id="ce17"><pre>Input: intervals = <span class="hljs-string">[[1,3],[6,9]]</span>, newInterval = [<span class="hljs-number">2</span>,<span class="hljs-number">5</span>]</pre></div><div id="4b73"><pre>Output: <span class="hljs-string">[[1,5],[6,9]]</span></pre></div><div id="366e"><pre><span class="hljs-attribute">Example</span> <span class="hljs-number">2</span>:</pre></div><div id="830a"><pre>Input: intervals = <span class="hljs-comment">[<span class="hljs-comment">[1,2]</span>,<span class="hljs-comment">[3,5]</span>,<span class="hljs-comment">[6,7]</span>,<span class="hljs-comment">[8,10]</span>,<span class="hljs-comment">[12,16]</span>]</span>, newInterval = <span class="hljs-comment">[4,8]</span></pre></div><div id="b8f8"><pre>Output: <span class="hljs-string">[[1,2],[3,10],[12,16]]</span></pre></div><div id="f33c"><pre><span class="hljs-symbol">Explanation</span>: <span class="hljs-symbol">Because</span> the new interval [<span class="hljs-number">4</span>,<span class="hljs-number">8</span>] overlaps with [<span class="hljs-number">3</span>,<span class="hljs-number">5</span>],[<span class="hljs-number">6</span>,<span class="hljs-number">7</span>],[<span class="hljs-number">8</span>,<span class="hljs-number">10</span>].</pre></div><h1 id="b02e">Observations</h1><p id="1118">Let’s make sure we understand the question first.</p><p id="e633">We see that in the first example, [1,3] and [2,5] are overlapping intervals. We can merge these to make [1,5].</p><p id="5bc2">What operation are we doing here? How do we know that these intervals overlap?</p><p id="1338">A key observation is that the second element of the first interval is greater than the first element of the second interval. (Note an overlapping interval could also have these two values be equal!)</p><p id="51da">Another key observation is that we need these intervals to be sorted by their first value in ascending order. For example, if I have [[3,5], [1,1]], and I apply the rule above, I would mistakenly think these two intervals overlap. However, if I’m looking at [[1,1],[3,5]], then apply the above rule, I know these intervals don’t overlap.</p><p id="adf0">So, let’s first sort the array by the first value in each interval, in ascending order.</p><h1 id="9c84">Algorithm</h1><p id="c85c">How should we go about processing this array?</p

Options

<p id="71fe">Since we have a *new* interval to insert, let’s think about when we would need to insert it. When do we *not* need to worry about merging intervals?</p><p id="a9d4">Looking at the second example:</p><p id="7289">intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]</p><p id="f6b6">How do I know that I don’t need to touch the [1,2] interval? Well, the start of my new interval (4) is greater than the end of that interval (2), so I don’t need to worry about [1,2].</p><p id="880c">But I do know there’s some merging going on with the [3,5], because the start of my new interval (4) is less than 5.</p><p id="22ed">So for the intervals that are strictly before my new interval, I can just append those to my result output first.</p><p id="8ae9">Now, when we get to the part where we *do* need to merge, then I’ll apply the merging logic I had above: while the end of my new interval is greater than or equal to the start of the next interval, I’ll take the minimum of the starts and maximum of the ends to get my merged interval.</p><p id="0fdc">How do I know when to stop my merging logic?</p><p id="b42c">Well, when the end of my merged interval is less than the start of the next interval to look at, then I know I can just append the rest of the intervals in the original array to my output, and return that.</p><h1 id="e9cd">Code</h1><p id="5725">Let’s see how the code looks:</p><figure id="ca99"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*_YEq6llxllmaGGCe5QAzlQ.png"><figcaption></figcaption></figure><h1 id="c419">Algorithmic Complexity</h1><p id="4518">Runtime complexity: O(N log N), because we’re using a sort.</p><p id="c9a5">Space complexity: O(N), because we’re making a new array to hold the output, which is the size of the original input with an additional interval in it.</p><p id="51af"><i>For more articles like this, <a href="https://medium.com/@SantalTech">follow me</a> on Medium. Not a member yet? <a href="https://medium.com/@SantalTech/membership">Join the community.</a></i>

<i>Want more software engineering interview guides and coding question tips? Check out all of my writing organized by topic <a href="https://readmedium.com/santals-writing-index-a179642e3e31">in this article.</a></i></p><p id="ceda"><i>If you have any requests for what I should write, please let me know!</i></p></article></body>

Google Technical Phone Screen Question: Insert Interval

Today’s problem involves merging intervals. This is a medium-difficulty problem that comes up frequently in Google technical interviews.

https://unsplash.com/photos/XOZgLEkB1ig

The problem is:

You are given an array of non-overlapping intervals intervals where intervals[i] = [start_i, end_i] represent the start and the end of the ith interval and intervals is sorted in ascending order by start_i. 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_i and intervals still does not have any overlapping intervals (merge overlapping intervals if necessary).
Return intervals after the insertion.
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].

Observations

Let’s make sure we understand the question first.

We see that in the first example, [1,3] and [2,5] are overlapping intervals. We can merge these to make [1,5].

What operation are we doing here? How do we know that these intervals overlap?

A key observation is that the second element of the first interval is greater than the first element of the second interval. (Note an overlapping interval could also have these two values be *equal*!)

Another key observation is that we need these intervals to be sorted by their first value in ascending order. For example, if I have [[3,5], [1,1]], and I apply the rule above, I would mistakenly think these two intervals overlap. However, if I’m looking at [[1,1],[3,5]], then apply the above rule, I know these intervals don’t overlap.

So, let’s first sort the array by the first value in each interval, in ascending order.

Algorithm

How should we go about processing this array?

Since we have a *new* interval to insert, let’s think about when we would need to insert it. When do we *not* need to worry about merging intervals?

Looking at the second example:

intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]

How do I know that I don’t need to touch the [1,2] interval? Well, the start of my new interval (4) is greater than the end of that interval (2), so I don’t need to worry about [1,2].

But I do know there’s some merging going on with the [3,5], because the start of my new interval (4) is less than 5.

So for the intervals that are strictly before my new interval, I can just append those to my result output first.

Now, when we get to the part where we *do* need to merge, then I’ll apply the merging logic I had above: while the end of my new interval is greater than or equal to the start of the next interval, I’ll take the minimum of the starts and maximum of the ends to get my merged interval.

How do I know when to stop my merging logic?

Well, when the end of my merged interval is less than the start of the next interval to look at, then I know I can just append the rest of the intervals in the original array to my output, and return that.

Code

Let’s see how the code looks:

Algorithmic Complexity

Runtime complexity: O(N log N), because we’re using a sort.

Space complexity: O(N), because we’re making a new array to hold the output, which is the size of the original input with an additional interval in it.

For more articles like this, follow me on Medium. Not a member yet? Join the community. Want more software engineering interview guides and coding question tips? Check out all of my writing organized by topic in this article.

If you have any requests for what I should write, please let me know!

Google
Coding Interview
Leetcod
Technical Interview
Software Engineering
Recommended from ReadMedium