Overlap Area of Two Rectangles
Updated
Problem statement
We describe a rectangle by four whole numbers [x1, y1, x2, y2]: it is made of all points whose x-coordinate is between x1 and x2 and whose y-coordinate is between y1 and y2, ends included. Its sides are therefore horizontal or vertical.
You are given two such rectangles, rec1 and rec2. Return the area of the part of the plane that both of them cover. When they have no point in common, or their common points form just a line segment or a single point, return 0.
Function
overlapArea(rec1: int[], rec2: int[]) → intExamples
Example 1
rec1 = [3,1,9,6]rec2 = [-2,-1,5,4]return = 6Going left to right, the shared part starts at the larger left edge, max(3, -2) = 3, and stops at the smaller right edge, min(9, 5) = 5, so it is 2 units wide. Going bottom to top, it starts at max(1, -1) = 1 and stops at min(6, 4) = 4, so it is 3 units tall. The area is 2 × 3 = 6.
Example 2
rec1 = [4,-6,7,-2]rec2 = [-5,1,0,3]return = 0rec2 ends at x = 0, before rec1 begins at x = 4, so no vertical line crosses both rectangles and they have no point in common. The answer is 0. (Subtracting edges anyway gives a width of 0 - 4 = -4 and a height of -2 - 1 = -3. Their product, 12, is not an area.)
Example 3
rec1 = [-4,3,2,8]rec2 = [-1,-2,6,3]return = 0The bottom side of rec1 and the top side of rec2 both lie on the line y = 3. Along that line, the two sides overlap from x = -1 to x = 2. That overlap is only a line segment, which has no area, so the answer is 0.
Constraints
- Each of
rec1andrec2holds exactly 4 numbers. - Inside one rectangle,
x1 < x2andy1 < y2, so its width and height are positive. - Every number lies between
-10^4and10^4, inclusive.
More Nutanix Onsite Interview questions
- Design Hit CounterONSITE INTERVIEW · Seen Oct 2026
- Peak Process MemoryONSITE INTERVIEW · Seen Aug 2026
- Minimum Operations to Transform ArrayONSITE INTERVIEW · Seen Jun 2026
- Count Connected Components in an Undirected GraphONSITE INTERVIEW · Seen Aug 2025
- LRU CacheONSITE INTERVIEW · Seen Aug 2025
- Running Median of a Multilevel Linked List in Level OrderONSITE INTERVIEW · Seen Aug 2025
- Custom Array IteratorONSITE INTERVIEW · Seen Dec 2024
- Collapse Runs of Repeated CharactersONSITE INTERVIEW · Seen Nov 2022
- Anagram Words in a SentenceONSITE INTERVIEW · Seen Apr 2022
- Sudoku SolverONSITE INTERVIEW · Seen Feb 2022
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
- 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