DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
Back to Arena
medium
Array

Insert Interval

**Problem Statement:** You are given an array of non-overlapping intervals `intervals` where `intervals[i] = [starti, endi]` represent the start and the end of the `i`th interval and `intervals` is sorted in ascending order by `starti`. You are also given an interval `newInterval`. Insert `newInterval` into `intervals` such that `intervals` is still sorted in ascending order by `starti` and `intervals` still does not have any overlapping intervals. **Hint / Expected Approach:** Single-pass before/overlap/after regions **Edge Cases to Consider:** - (1) New interval before all - (2) Absorbs all intervals - (3) Empty input list

Examples

Example 1
Input: [[1,3],[6,9]], [2,5]
Output: [[1,5],[6,9]]
Example 2
Input: [[1,2],[3,5],[6,7],[8,10],[12,16]], [4,8]
Output: [[1,2],[3,10],[12,16]]
Example 3
Input: [], [5,7]
Output: [[5,7]]

Constraints

  • ▪1 <= nums.length <= 10^4
  • ▪-10^9 <= nums[i] <= 10^9
  • ▪Time complexity should be O(n) or O(n log n)

Watch Out For Edge Cases

  • ▪New interval before all
  • ▪Absorbs all intervals
  • ▪Empty input list
Frequently Asked At
GoogleAmazonMicrosoft