This was a little bit fiddly with some edge cases to consider. Iterate through the intervals array attempting to insert the new range or merge it. So at every step we need to check if the new range is before the current range or overlapping. There are a few different overlap conditions to consider. Also check to see if we need to insert the new entry at the back of the array.
Code from above:
And, slightly cleaned up version: