File: rectangle-overlap/solution.py

Date: 2026-06-06

Time: 18:40

rectangle-overlap/solution.py

Purpose

This file solves LeetCode 836 - Rectangle Overlap. It determines whether two axis-aligned rectangles have a positive-area intersection. It's one of hundreds of LeetCode solutions in the repo, each following the same directory structure (solution.py, test_solution.py, plan.md, review.md).

Key Components

racecar(rec1, rec2) -> bool — The sole function. Despite the name racecar (which is wrong — likely a copy-paste artifact from another problem or a generator bug), it implements the rectangle overlap check. It takes two rectangles, each represented as [x1, y1, x2, y2] where (x1, y1) is the bottom-left corner and (x2, y2) is the top-right corner.

Patterns

The solution uses the negation-of-non-overlap approach, though expressed directly as the overlap condition. Two rectangles overlap if and only if they overlap on both axes independently. The four comparisons are:

| Condition | Meaning |

|-----------|---------|

| rec1[0] < rec2[2] | rec1's left edge is left of rec2's right edge |

| rec2[0] < rec1[2] | rec2's left edge is left of rec1's right edge |

| rec1[1] < rec2[3] | rec1's bottom edge is below rec2's top edge |

| rec2[1] < rec1[3] | rec2's bottom edge is below rec1's top edge |

All four must hold simultaneously. Strict < (not <=) ensures the intersection has positive area — touching edges or corners don't count.

Dependencies

Imports: None. Pure function with no external dependencies.

Imported by: rectangle-overlap/test_solution.py and, according to the "Imported By" list, hundreds of other test files. That massive import list is almost certainly an artifact of the test harness importing a shared module or test runner, not direct usage of racecar from unrelated problems.

Flow

Straightforward single-expression evaluation. Python's short-circuit and means it bails on the first False condition. No loops, no branching, no mutation. O(1) time and space.

Invariants

Error Handling

None. The function trusts its inputs per LeetCode convention. Passing lists shorter than 4 elements would raise an IndexError from Python's list indexing.

Notable Issue

The function is named racecar instead of something like isRectangleOverlap. This is clearly a bug — likely from a code generation pipeline that didn't set the function name correctly. The docstring and logic are correct for rectangle overlap, but any caller must import/call it as racecar. This won't affect LeetCode submission (which matches by class/method signature), but it makes the standalone code confusing.

Topics to Explore

Beliefs