Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

189. Insert Interval

MediumDay 56 of 60Greedy & Intervals

You are given a list of non-overlapping intervals sorted by start, and one more interval newInterval. Insert newInterval so the list stays sorted and non-overlapping, merging intervals where needed, and return the new list.

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]]
[4,8] overlaps [3,5], [6,7] and [8,10].
Constraints
  • 0 <= intervals.length <= 10^4
  • intervals is sorted by start and has no overlaps
  • 0 <= start <= end <= 10^5
Hints
Frequently asked at: Google, Amazon, LinkedIn, Meta
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min