Magicsheet logo

Rectangle Overlap

Easy
38.4%
Updated 6/1/2025

Rectangle Overlap

What is this problem about?

The Rectangle Overlap problem asks whether two axis-aligned rectangles overlap (share any positive area). Two rectangles overlap if their x-ranges and y-ranges both intersect with positive length. This easy coding problem tests concise geometric condition evaluation. The math and geometry interview pattern is demonstrated.

Why is this asked in interviews?

Microsoft, Meta, Amazon, Google, and Adobe ask this as a geometry warm-up. It tests the ability to express the overlap condition concisely: two rectangles overlap if and only if they don't NOT overlap in either dimension.

Algorithmic pattern used

Non-overlap complement. Rectangles do NOT overlap if: rec2[0] >= rec1[2] (rec2 is to the right), OR rec2[2] <= rec1[0] (rec2 is to the left), OR rec2[1] >= rec1[3] (rec2 is above), OR rec2[3] <= rec1[1] (rec2 is below). Return NOT of these conditions.

Or equivalently: overlap iff rec1[0] < rec2[2] AND rec2[0] < rec1[2] AND rec1[1] < rec2[3] AND rec2[1] < rec1[3].

Example explanation

rec1=[0,0,2,2], rec2=[1,1,3,3].

  • x: 0 < 3 ✓ and 1 < 2 ✓.
  • y: 0 < 3 ✓ and 1 < 2 ✓. All conditions met → true (overlapping).

rec1=[0,0,1,1], rec2=[1,0,2,1]. x: 0 < 2 ✓ but 1 < 1? No → false (adjacent, not overlapping).

Common mistakes candidates make

  • Using ≤ instead of < (adjacent rectangles don't overlap, must be strict).
  • Mixing up which coordinate is which (bottom-left vs top-right).
  • Checking only one dimension.
  • Using area-based check (computing intersection area) which is unnecessary.

Interview preparation tip

Rectangle Overlap uses the non-overlap complement pattern: it's easier to check when they DON'T overlap (one is entirely left/right/above/below) than when they do. The complement of 4 non-overlap conditions gives overlap. Memorize the strict inequality condition: overlap requires strictly overlapping intervals in BOTH dimensions. Practice with touching rectangles (they don't count as overlapping per this strict definition).

Similar Questions