Overlapping Rectangles
viaLeetCode
Problem Given two axis-parallel rectangles by their top-left and bottom-right corners (L1,R1 and L2,R2), determine whether they overlap; sharing an edge counts as overlapping.
Input / Output
- Input: points L1, R1, L2, R2 (each with x, y).
- Output: boolean.
Constraints
- O(1) — this is a logic question, not an algorithms question; getting the conditions and inclusivity right is the test.
Example
- L1=(0,10), R1=(10,0), L2=(5,5), R2=(15,0) → true. L2=(11,10), R2=(20,0) → false (strictly to the right).
Expected approach
- Check the two separation axioms and negate: rectangles do NOT overlap iff one is entirely to the left of the other (R1.x < L2.x or R2.x < L1.x) or entirely above/below (L1.y < R2.y or L2.y < R1.y, with y decreasing downward as given). Overlap = !(separated). Shared edges allowed → use strict inequalities in the separation test. Degenerate rectangles (zero width/height) and coordinate-system direction are the edge cases to call out.
asked …