Interval List Intersection Problem

You are provided with two sorted lists of closed intervals, INTERVAL1 and INTERVAL2. A closed interval [x, y] (x < y) signifies the set of real numbers z such that x <= z <= y.

Your task is to find the intersections of these two interval lists.

The intersection of two closed intervals is a set of real numbers represented as a closed interval or an empty set. For instance, the intersection of [0, 2] and [1, 3] is [1, 2].

Input:

The first line contains an integer 'T', which represents the number of test cases. Each test case consists of: 
- First line with two integers 'N1' and 'N2', indicating the sizes of 'INTERVAL1' and 'INTERVAL2'.
- Second line with '2 * N1' integers representing the intervals in 'INTERVAL1'.
- Third line with '2 * N2' integers representing the intervals in 'INTERVAL2'.

Output:

For each test case, output a list or array containing the intervals of intersections of 'INTERVAL1' and 'INTERVAL2'.
Output each test case result on a new line.

Example:

Input:
INTERVAL1 = [[0, 5], [7, 9], [10, 11]]
INTERVAL2 = [[0, 2], [3, 7], [12, 15]]
Output:
[[0, 2], [3, 5], [7, 7]]

Constraints:

  • 1 <= T <= 100
  • 0 <= N1 <= 5000
  • 0 <= N2 <= 5000
  • 0 <= INTERVAL1[i][0] <= 105
  • INTERVAL1[i][0] < INTERVAL1[i][1] <= 105
  • 0 <= INTERVAL2[i][0] <= 105
  • INTERVAL2[i][0] < INTERVAL2[i][1] <= 105
Note:
Your implementation should not print anything. Just implement the function to return the result.
AnswerBot
4mo

Find the intersections of two sorted lists of closed intervals.

  • Iterate through both interval lists to find intersections

  • Compare the intervals and find the common range

  • Handle cases where there is no in...read more

Help your peers!
Select
Add answer anonymously...

American Express Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Left View of a Binary Tree Problem Statement Given a binary tree, your task is t...read more
A Software Developer Intern was asked Q. Minimum Number of Swaps to Sort an Array Find the minimum number of swaps requir...read more
A Software Developer Intern was asked Q. Maximum Non-Adjacent Subsequence Sum Given an array of integers, determine the m...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Left View of a Binary Tree Problem Statement Given a binary tree, your task is t...read more
A Software Developer Intern was asked Q2. Minimum Number of Swaps to Sort an Array Find the minimum number of swaps requir...read more
A Software Developer Intern was asked Q3. Maximum Non-Adjacent Subsequence Sum Given an array of integers, determine the m...read more
American Express Software Developer Intern 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