Common Part of Two Rectangles
Updated
Problem statement
You are given two rectangles, rect1 and rect2. Their sides are parallel to the x and y axes. Each one is an array [x1, y1, x2, y2], where (x1, y1) is the bottom-left corner and (x2, y2) is the top-right corner.
Return the rectangle where the two overlap, in the same [x1, y1, x2, y2] form. If the overlap has no area, return an empty array. This includes rectangles that are apart and rectangles that only touch along an edge or at a corner.
Function
intersectRectangles(rect1: int[], rect2: int[]) → int[]Examples
Example 1
rect1 = [-4,1,5,7]rect2 = [2,-3,10,4]return = [2,1,5,4]Across, the shared part runs from max(-4, 2) = 2 to min(5, 10) = 5. Up and down, it runs from max(1, -3) = 1 to min(7, 4) = 4. Both ranges have positive length, so the overlap is [2, 1, 5, 4].
Example 2
rect1 = [1,1,4,6]rect2 = [4,2,9,3]return = []The first rectangle ends at x = 4 and the second starts at x = 4. They share only part of a vertical line, which has no area, so the answer is an empty array.
Example 3
rect1 = [-10,-10,10,10]rect2 = [-3,2,1,8]return = [-3,2,1,8]The second rectangle lies fully inside the first, so the overlap is the second rectangle itself.
Constraints
rect1.length == rect2.length == 4-10^9 <= rect1[i], rect2[i] <= 10^9- In each rectangle,
x1 < x2andy1 < y2.
More Expedia Onsite Interview questions
- Scramble StringONSITE INTERVIEW · Seen Sep 2026
- Smallest Sufficient TeamONSITE INTERVIEW · Seen Sep 2026
- Check Anagram Pairs Index by IndexONSITE INTERVIEW · Seen Oct 2025
- Fresh Oranges Left After Rotting StopsONSITE INTERVIEW · Seen Oct 2025
- Longest Increasing SubsequenceONSITE INTERVIEW · Seen Oct 2025
- Lowest Common Ancestor with Repeated and Missing ValuesONSITE INTERVIEW · Seen Oct 2025
- Shortest Substring with Exactly K OnesONSITE INTERVIEW · Seen Oct 2025
- Middle Element of a Linked ListONSITE INTERVIEW · Seen Nov 2024
- Minimum in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Sep 2024
- Mirror Image Binary TreesONSITE INTERVIEW · Seen Sep 2024
Similar questions at other companies
- Rectangle OverlapMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Correct Three Protractor ReadingsSusquehanna International Group (SIG) · ONSITE INTERVIEWSusquehanna International Group (SIG) interview questions
- Overlap Area of Two RectanglesNutanix · ONSITE INTERVIEWNutanix interview questions
- Optimal Central WarehouseGoogle · ONSITE INTERVIEWGoogle interview questions
- Max Points on a LineLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Rasterize a Circle with Integer PixelsPure Storage · ONSITE INTERVIEWPure Storage interview questions
- Point Inside a TriangleSambaNova Systems · PHONE SCREEN · ONSITE INTERVIEWSambaNova Systems interview questions
- Circle Intersection in Three DimensionsNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Range of a Third CorrelationTower Research Capital · PHONE SCREEN
- Three Points in a Semicircle: Monte Carlo EstimateSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions
Expedia interview experiences
- Expedia Intern Final Round in 2021: 28 Experiences (Part 4 of 6)
- Expedia Intern Finals Before 2020: 14 Experiences (Part 6 of 6)
- Expedia Intern Final Round in 2020: 24 Experiences (Part 5 of 6)
- Expedia Intern Final Round in 2022: 15 Experiences (Part 3 of 6)
- Expedia Intern Final Round in 2023: 17 Experiences (Part 2 of 6)
- Expedia SWE Intern Final Round: 126 Experiences (Part 1 of 6)