Rectangle Overlap

An axis-aligned rectangle is given as [x1, y1, x2, y2], where (x1, y1) is its bottom-left corner and (x2, y2) its top-right corner. Two rectangles overlap when the area of their intersection is positive; rectangles that only share an edge or a corner do not overlap. Given rec1 and rec2, return true if they overlap.

30:00

Rectangle Overlap
easy
Free written solution
Topics
Companies

An axis-aligned rectangle is given as [x1, y1, x2, y2], where (x1, y1) is its bottom-left corner and (x2, y2) its top-right corner. Two rectangles overlap when the area of their intersection is positive; rectangles that only share an edge or a corner do not overlap. Given rec1 and rec2, return true if they overlap.

Example 1:
Input: {"rec1":[0,0,2,2],"rec2":[1,1,3,3]}
Output: true
Constraints:
  • rec1.length == 4

  • rec2.length == 4

  • −109≤-10^9 \leq rec1[i], rec2[i] ≤109\leq 10^9

  • rec1 and rec2 each describe a valid rectangle with non-zero area.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"rec1":[0,0,2,2],"rec2":[1,1,3,3]}

Two rectangles overlap exactly when their shadows on the x axis overlap and their shadows on the y axis overlap. Check each axis separately.

the plane and each rectangle's shadow on x and y

01230123rec1rec2xy

■ rec1 [0, 0, 2, 2]

■ rec2 [1, 1, 3, 3]

the two tests

left = ?
right = ?
left < right: ?
bottom = ?
top = ?
bottom < top: ?
Variables
VariableValue
rec1[0,0,2,2]
rec2[1,1,3,3]
DepthFunction Call
Stack empty
0/7