Rectangle Overlap: Axis Projection Check
LeetCode 836, Rectangle Overlap, asks whether two axis-aligned rectangles overlap. Each rectangle is represented as a list [x1, y1, x2, y2] where (x1, y1) is the bottom-left corner and (x2, y2) is the top-right corner. Two rectangles overlap if the area of their intersection is positive, meaning shared edges or corners alone do not count.
The problem
You are given two rectangles rec1 and rec2, each described by four integers. Return True if they overlap and False otherwise.
rec1 = [0, 0, 2, 2]
rec2 = [1, 1, 3, 3]
# Output: True (the rectangles share a 1x1 area)
rec1 = [0, 0, 1, 1]
rec2 = [1, 0, 2, 1]
# Output: False (they touch at edge x=1, but share no area)
The key insight is that two rectangles overlap if and only if they overlap on both the x-axis and the y-axis independently. If either axis has no overlap, the rectangles cannot share any area. This is a direct application of the separating axis theorem for axis-aligned rectangles.
Think of it this way: project both rectangles onto the x-axis. You get two 1D intervals. If those intervals do not overlap, the rectangles are separated horizontally and cannot intersect. Do the same for the y-axis. Only when both projections overlap do the 2D rectangles actually share area.
The solution
class Solution:
def isRectangleOverlap(self, rec1: list[int], rec2: list[int]) -> bool:
return rec1[0] < rec2[2] and rec2[0] < rec1[2] and rec1[1] < rec2[3] and rec2[1] < rec1[3]
Step-by-step walkthrough
Let's trace through rec1 = [0, 0, 2, 2] and rec2 = [1, 1, 3, 3].
Step 1: Identify the two rectangles and their coordinates
rec1 = [0,0,2,2] has bottom-left (0,0) and top-right (2,2). rec2 = [1,1,3,3] has bottom-left (1,1) and top-right (3,3).
Step 2: Check x-axis overlap
x1 of rec1 (0) < x2 of rec2 (3) AND x1 of rec2 (1) < x2 of rec1 (2). Both true, so the x-projections overlap.
Step 3: Check y-axis overlap
y1 of rec1 (0) < y2 of rec2 (3) AND y1 of rec2 (1) < y2 of rec1 (2). Both true, so the y-projections overlap.
Step 4: Combine both checks. Overlap exists when BOTH axes overlap.
X overlaps? Yes. Y overlaps? Yes. Both true, so return True.
The four comparisons in the return statement encode two separate checks. The first two, rec1[0] < rec2[2] and rec2[0] < rec1[2], verify that the x-projections overlap. The last two, rec1[1] < rec2[3] and rec2[1] < rec1[3], verify that the y-projections overlap. All four must be true for the rectangles to share area.
Why strict < instead of <=? Because the problem says overlap requires positive area. If two rectangles share only an edge, one of these comparisons becomes an equality rather than strict less-than, and the expression correctly returns False.
Complexity analysis
| Metric | Value | Why |
|---|---|---|
| Time | O(1) | Four comparisons, no loops or data structures |
| Space | O(1) | No extra memory allocated |
This is optimal. You need to inspect at least a few coordinates to determine overlap, and this solution does exactly that with a constant number of operations.
The building blocks
Axis-aligned interval overlap
The core building block here is 1D interval overlap detection. Two intervals [a, b] and [c, d] overlap (with positive length) when a < d and c < b. This is the same condition you see in scheduling problems, sweep-line algorithms, and other rectangle geometry problems.
Once you internalize the 1D check, the 2D rectangle version is just two copies of it, one per axis.
Separating axis theorem
For axis-aligned rectangles, the separating axis theorem simplifies to: if you can find an axis (x or y) along which the projections of the two shapes do not overlap, then the shapes themselves do not overlap. Since there are only two axes to check, this gives you a clean, constant-time test.
For general convex polygons, you would need to check more potential separating axes. But axis-aligned rectangles are the simplest case, and the theorem reduces to the four-comparison one-liner above.
Edge cases
- Touching edges.
rec1 = [0,0,1,1]andrec2 = [1,0,2,1]share the edge atx = 1, but the intersection area is zero. The checkrec1[2] > rec2[0]becomes1 > 1, which is false, so the function correctly returnsFalse. - Touching corners.
rec1 = [0,0,1,1]andrec2 = [1,1,2,2]share only the point(1,1). Both the x and y checks fail on the boundary, returningFalse. - One rectangle inside the other.
rec1 = [0,0,4,4]andrec2 = [1,1,2,2]. All four conditions hold with comfortable margins, returningTrue. - Zero-area rectangles. If
rec1 = [0,0,0,1](a vertical line segment), it has zero width. The x-axis check0 < rec2[2]andrec2[0] < 0will fail for any rectangle that does not span negative x-coordinates. The problem states rectangles can be points or lines, and since these have no area, they cannot produce a positive-area overlap. - Identical rectangles.
rec1 = rec2 = [0,0,2,2]. All four strict inequalities hold (each<compares 0 and 2), returningTrue.
From understanding to recall
This problem is deceptively simple. The solution is a single line, but under interview pressure it is easy to mix up which coordinates to compare, or to use <= when you need <. The four conditions are symmetric in a specific way: you compare the left edge of one rectangle against the right edge of the other, and vice versa, for both axes.
Practicing this once or twice with spaced repetition locks the pattern into memory so you can write it without hesitation.
Related posts
- Rectangle Area applies the same interval overlap idea but goes further, computing the actual overlap area for inclusion-exclusion
- Insert Interval uses 1D interval overlap detection as its core building block in a sweep-line context
- Merge Intervals is the foundational interval merging problem that trains the same overlap intuition
The axis projection technique you learned here is one of the most reusable ideas in computational geometry. Any time you work with axis-aligned shapes, checking each dimension independently is your first move.