KatInterview

223. Rectangle Area

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks for the total area covered by two rectangles in a 2D plane. The rectangles are defined by their bottom-left and top-right corners. A straightforward approach of simply adding the areas of the two rectangles would double-count any overlapping region. The key to solving this problem is to use the Principle of Inclusion-Exclusion. This principle states that for two sets (or areas) A and B, the area of their union is:

Solution

Rectangle Area in Python

class Solution:
    def computeArea(self, ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) -> int:
        # Calculate the area of the first rectangle.
        # Python integers handle arbitrary size, so no overflow issues.
        area1 = (ax2 - ax1) * (ay2 - ay1)
        
        # Calculate the area of the second rectangle.
        area2 = (bx2 - bx1) * (by2 - by1)
        
        # Calculate the overlap width.
        # The overlap interval on the x-axis is from max(ax1, bx1) to min(ax2, bx2).
        # max(0, ...) ensures the width is at least 0 if there is no overlap.
        overlap_width = max(0, min(ax2, bx2) - max(ax1, bx1))
        
        # Calculate the overlap height.
        # The overlap interval on the y-axis is from max(ay1, by1) to min(ay2, by2).
        overlap_height = max(0, min(ay2, by2) - max(ay1, by1))
        
        # Calculate the area of the overlap.
        overlap_area = overlap_width * overlap_height
        
        # The total area is the sum of individual areas minus the overlap area
        # (Principle of Inclusion-Exclusion).
        return area1 + area2 - overlap_area

Rectangle Area in Java

class Solution {
    public int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
        // Calculate the area of the first rectangle.
        // Use long for area calculation as a good practice to prevent potential overflow.
        long area1 = (long)(ax2 - ax1) * (ay2 - ay1);
        
        // Calculate the area of the second rectangle.
        long area2 = (long)(bx2 - bx1) * (by2 - by1);
        
        // Calculate the overlap width. 
        // The overlap interval on the x-axis is [max(ax1, bx1), min(ax2, bx2)].
        // Math.max(0, ...) ensures width is non-negative.
        int overlapWidth = Math.max(0, Math.min(ax2, bx2) - Math.max(ax1, bx1));
        
        // Calculate the overlap height.
        // The overlap interval on the y-axis is [max(ay1, by1), min(ay2, by2)].
        int overlapHeight = Math.max(0, Math.min(ay2, by2) - Math.max(ay1, by1));
        
        // Calculate the area of the overlap.
        long overlapArea = (long)overlapWidth * overlapHeight;
        
        // Total area is the sum of individual areas minus the overlap area
        // (Principle of Inclusion-Exclusion). Cast the final result back to int.
        return (int)(area1 + area2 - overlapArea);
    }
}

Rectangle Area in C++

class Solution {
public:
    int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
        // Calculate area of rectangle 1. Using long long to prevent potential overflow,
        // although for this problem's specific constraints, int would suffice.
        long long area1 = (long long)(ax2 - ax1) * (ay2 - ay1);
        
        // Calculate area of rectangle 2.
        long long area2 = (long long)(bx2 - bx1) * (by2 - by1);
        
        // Calculate the overlapping width. The result is non-negative.
        // The overlap starts at the maximum of the left edges and ends at the minimum of the right edges.
        int overlapWidth = std::max(0, std::min(ax2, bx2) - std::max(ax1, bx1));
        
        // Calculate the overlapping height. The result is non-negative.
        // The overlap starts at the maximum of the bottom edges and ends at the minimum of the top edges.
        int overlapHeight = std::max(0, std::min(ay2, by2) - std::max(ay1, by1));
        
        // Calculate the area of the overlapping region. Cast to long long for safety during multiplication.
        long long overlapArea = (long long)overlapWidth * overlapHeight;
        
        // Total area is the sum of individual areas minus the overlap area (Principle of Inclusion-Exclusion).
        return (int)(area1 + area2 - overlapArea);
    }
};

Rectangle Area in TypeScript

function computeArea(ax1: number, ay1: number, ax2: number, ay2: number, bx1: number, by1: number, bx2: number, by2: number): number {
    // Calculate the area of the first rectangle.
    // TypeScript's `number` type is a 64-bit float, sufficient for these calculations.
    const area1 = (ax2 - ax1) * (ay2 - ay1);
    
    // Calculate the area of the second rectangle.
    const area2 = (bx2 - bx1) * (by2 - by1);
    
    // Calculate the overlap width.
    // The overlap interval on the x-axis is from Math.max(ax1, bx1) to Math.min(ax2, bx2).
    // Math.max(0, ...) ensures the width is non-negative if there is no overlap.
    const overlapWidth = Math.max(0, Math.min(ax2, bx2) - Math.max(ax1, bx1));
    
    // Calculate the overlap height.
    // The overlap interval on the y-axis is from Math.max(ay1, by1) to Math.min(ay2, by2).
    const overlapHeight = Math.max(0, Math.min(ay2, by2) - Math.max(ay1, by1));
    
    // Calculate the area of the overlap.
    const overlapArea = overlapWidth * overlapHeight;
    
    // Total area is the sum of individual areas minus the overlap area
    // using the Principle of Inclusion-Exclusion.
    return area1 + area2 - overlapArea;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.