Rectangle Overlap
Updated
Problem statement
Each rectangle has sides parallel to the axes and is given as [x1, y1, x2, y2], where (x1, y1) is its bottom-left corner and (x2, y2) is its top-right corner.
Given two rectangles rec1 and rec2, return true if they intersect and false otherwise. Two rectangles intersect when the region they share has a positive area. Rectangles that only touch along an edge or at a corner do not intersect.
Function
isRectangleOverlap(rec1: int[], rec2: int[]) → booleanExamples
Example 1
rec1 = [0,0,4,3]rec2 = [2,1,6,5]return = trueThe x-ranges 0 to 4 and 2 to 6 share 2 to 4, and the y-ranges 0 to 3 and 1 to 5 share 1 to 3. The rectangles share the 2 by 2 square from (2, 1) to (4, 3), so the answer is true.
Example 2
rec1 = [0,0,2,2]rec2 = [2,0,4,2]return = falseThe rectangles meet along the line x = 2 but share no area, so the answer is false.
Example 3
rec1 = [-1,-5,1,5]rec2 = [-5,-1,5,1]return = trueThe rectangles form a plus sign. No corner of either rectangle is inside the other, but they share the square from (-1, -1) to (1, 1), so the answer is true.
Constraints
rec1.length == rec2.length == 4-10^9 <= rec1[i], rec2[i] <= 10^9x1 < x2andy1 < y2for each rectangle.
More Microsoft Onsite Interview questions
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
- Totals from a Tiered Rate TableONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Correct Three Protractor ReadingsSusquehanna International Group (SIG) · ONSITE INTERVIEWSusquehanna International Group (SIG) interview questions
- Overlap Area of Two RectanglesNutanix · ONSITE INTERVIEWNutanix interview questions
- Common Part of Two RectanglesExpedia · ONSITE INTERVIEWExpedia 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