This problem requires computing the combined area of two rectangles on a 2D plane. The challenge lies in correctly handling overlapping regions to avoid double counting. By calculating individual rectangle areas and subtracting the overlap, you get an accurate total using simple geometric reasoning.
Problem Statement
Given two axis-aligned rectangles in a 2D coordinate plane, return the total area covered by both rectangles. Each rectangle is defined by its bottom-left and top-right corners with integer coordinates.
The first rectangle has corners (ax1, ay1) and (ax2, ay2). The second rectangle has corners (bx1, by1) and (bx2, by2). The rectangles may overlap partially, fully, or not at all. Compute the combined area, ensuring overlapping regions are only counted once.
Examples
Example 1
Input: ax1 = -3, ay1 = 0, ax2 = 3, ay2 = 4, bx1 = 0, by1 = -1, bx2 = 9, by2 = 2
Output: 45
Example details omitted.
Example 2
Input: ax1 = -2, ay1 = -2, ax2 = 2, ay2 = 2, bx1 = -2, by1 = -2, bx2 = 2, by2 = 2
Output: 16
Example details omitted.
Constraints
- -104 <= ax1 <= ax2 <= 104
- -104 <= ay1 <= ay2 <= 104
- -104 <= bx1 <= bx2 <= 104
- -104 <= by1 <= by2 <= 104
Solution Approach
Compute Individual Rectangle Areas
Calculate the area of each rectangle separately using the formula (width * height) for both rectangles. Width is the difference between right and left x-coordinates, height is the difference between top and bottom y-coordinates.
Determine Overlapping Area
Find the horizontal and vertical overlap between the rectangles. Horizontal overlap is max(0, min(ax2, bx2) - max(ax1, bx1)), vertical overlap is max(0, min(ay2, by2) - max(ay1, by1)). Multiply these to get the overlapping area.
Combine Areas Safely
Add the two rectangle areas and subtract the overlap area. This ensures any shared region is counted only once, producing the correct total area covered by both rectangles.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
Time complexity is O(1) because only constant arithmetic operations are needed. Space complexity is O(1) since no additional data structures are used beyond a few variables for coordinates and overlap calculation.
What Interviewers Usually Probe
- Expect clear handling of overlapping regions.
- Look for edge cases where rectangles touch but do not overlap.
- Check that the solution avoids negative overlap or double counting.
Common Pitfalls or Variants
Common pitfalls
- Forgetting to subtract the overlapping area, leading to inflated totals.
- Incorrectly calculating overlap when rectangles do not intersect.
- Confusing width and height computations with coordinate differences.
Follow-up variants
- Computing total area for multiple rectangles, not just two.
- Handling rectangles with floating-point coordinates instead of integers.
- Finding the overlapping area only without computing total area.
How GhostInterview Helps
- Automatically verifies overlap calculations and combined area correctness.
- Highlights failure modes like missing or negative overlap handling.
- Provides a step-by-step breakdown of width, height, and area arithmetic for clarity.
Topic Pages
Related GhostInterview Pages
- LeetCode Interview Copilot - Use GhostInterview as a live solver when you want direct help with LeetCode-style coding questions.
- Coding Interview Assistant - See how GhostInterview supports array, string, linked list, graph, and tree interview workflows.
- How GhostInterview Works - Review the screenshot, reasoning, and answer flow before using the solver in a live interview.
FAQ
How do I compute the total area of two rectangles in the Rectangle Area problem?
Calculate each rectangle's area, compute any overlapping area, and subtract the overlap from the sum of both rectangles.
What if the rectangles do not overlap at all?
If there is no overlap, the overlap calculation will yield zero, and the total area is simply the sum of the two rectangle areas.
Can coordinates be negative?
Yes, rectangles can have negative coordinates as long as the bottom-left corner is less than or equal to the top-right corner.
How to handle rectangles touching at edges?
Edge-touching rectangles have zero overlap. Ensure your overlap formula returns max(0, overlap) to avoid negative values.
Does this approach work for more than two rectangles?
The same pattern applies, but you must iteratively calculate overlaps for all pairs to avoid double counting.
Need direct help with Rectangle Area instead of spending more time grinding it?
Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Rectangle Area from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.
Capture the prompt fast instead of rewriting the problem by hand.
Get the solution path, trade-offs, and complexity summary in one pass.
Stay outside captured layers on supported screen-share workflows.
Stay in the same pattern family
Find the maximum number of points on a straight line in a 2D plane using array scanning and hash lookup.
Open problem page#335 Self CrossingDetermine if a path defined by sequential distances on a 2D plane crosses itself using array and math reasoning.
Open problem page#391 Perfect RectangleDetermine if given axis-aligned rectangles form a perfect cover using array scanning and hash-based corner counting techniques.
Open problem page