Magicsheet logo

Rectangle Area

Medium
59.6%
Updated 6/1/2025

Rectangle Area

What is this problem about?

The Rectangle Area problem gives you two axis-aligned rectangles (each defined by bottom-left and top-right corners) and asks for the total area covered by both. This includes the overlap area counted only once. This medium coding problem uses geometric intersection computation. The math and geometry interview pattern is demonstrated.

Why is this asked in interviews?

Apple, Microsoft, Meta, Amazon, and Google ask this to test 2D geometry: area = area1 + area2 - intersection_area. Computing the intersection requires finding the overlapping x-range and y-range. It validates the ability to handle 2D rectangle geometry cleanly.

Algorithmic pattern used

Area union formula + intersection computation. Total = area1 + area2 - intersection. Intersection width = max(0, min(x2,x4) - max(x1,x3)). Intersection height = max(0, min(y2,y4) - max(y1,y3)). Intersection area = width × height.

Example explanation

Rect1: (0,0)→(2,2). Area1=4. Rect2: (1,1)→(3,3). Area2=4. Intersection: width=min(2,3)-max(0,1)=2-1=1. Height=min(2,3)-max(0,1)=1. Intersection area=1. Total = 4+4-1 = 7.

No overlap: Rect2=(3,3)→(5,5). Width=max(0,min(2,5)-max(0,3))=max(0,-1)=0. Intersection=0. Total=4+4=8.

Common mistakes candidates make

  • Forgetting to subtract intersection (double-counting overlap).
  • Negative intersection dimensions (must use max(0, ...)).
  • Confusing which corner is which for intersection bounds.
  • Integer overflow for large coordinates.

Interview preparation tip

Rectangle Area is a clean geometry problem. Memorize the formula: total = A1 + A2 - intersection, where intersection = max(0, min_right - max_left) × max(0, min_top - max_bottom). The max(0, ...) handles the no-overlap case automatically. Practice with different rectangle configurations to build intuition for overlap edge cases.

Similar Questions