MongoDB Logo

Asked in MongoDB

Merge Overlapping Intervals Problem Statement

You are given 'N' intervals, each defined by two integers, marking its boundaries. Your task is to merge all overlapping intervals and return a list of merged intervals sorted in ascending order.

Example:

Input:
5
start: [1, 3, 6, 10, 8]
end: [4, 5, 8, 12, 9]
Output:
[1, 5]
[6, 9]
[10, 12]
Explanation:

Intervals [1,4] and [3,5] overlap and merge into [1,5]. Similarly, [6,8] and [8,9] overlap and merge into [6,9]. [10,12] does not overlap with any other interval.

Input:

Input consists of multiple test cases. Each test case starts with a line containing an integer 'N', the number of intervals. The next line contains 'N' starting integers. The following line contains 'N' ending integers.

Output:

For each test case, print the merged intervals, each specified by two space-separated integers (start end) per line. Output each test case result on a new line.

Constraints:

  • 1 ≤ T ≤ 100
  • 1 ≤ N ≤ 1000
  • 0 ≤ start, finish ≤ 10^9
  • Duration per test case: 1 second
Be the first one to answer
Select
Add answer anonymously...
undefined 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