Insert Interval Problem Statement

You are provided with a list of 'N' non-overlapping intervals, each defined by two integers, 'start' and 'end', sorted in ascending order by 'start' values. Your task is to insert a given interval into the list so that all intervals remain in sorted order.

If the given interval overlaps with any existing intervals, you must merge all necessary intervals to maintain a list of only non-overlapping intervals.

Input:

The first line of input contains an integer ‘T’, the number of test cases.
The first line of each test case contains a positive integer ‘N’, the number of intervals in the list.
Subsequent ‘N’ lines contain two space-separated integers ‘start’ and ‘end’, representing existing intervals.
The following line contains two space-separated integers, ‘start’ and ‘end’ for the interval to be inserted.

Output:

For each test case, return the list of intervals after inserting the given interval.

Example:

Suppose the list of intervals is: [[1,3], [5,7], [8,12]] and you need to insert the interval [4,6].
Insert [4,6] in the list which results in merging [4,6] with [5,7] to form [4,7].
The final list is: [[1,3], [4,7], [8,12]]

Constraints:

  • 1 ≤ T ≤ 10
  • 1 ≤ N ≤ 5 * 104
  • 1 ≤ start ≤ end ≤ 105

Note:
You do not need to print anything; simply implement the function as specified.

AnswerBot
4mo

The task is to insert a given interval into a list of non-overlapping intervals while maintaining sorted order.

  • Iterate through the list of intervals to find the correct position to insert the given in...read more

Help your peers!
Select
Add answer anonymously...

S&P Global Software Developer interview questions & answers

A Software Developer was asked Q. Given an array of integers nums and an integer target, return indices of the two...read more
A Software Developer was asked Q. Given a singly linked list, detect if there is a loop in it.
A Software Developer was asked Q. Number Pattern Problem Statement Design a seating arrangement for a high-securit...read more

Popular interview questions of Software Developer

A Software Developer was asked Q1. Given a singly linked list, detect if there is a loop in it.
A Software Developer was asked Q2. Number Pattern Problem Statement Design a seating arrangement for a high-securit...read more
A Software Developer was asked Q3. Quick Sort Problem Statement Sort the given array of integers in ascending order...read more
S&P Global Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits