You are given two axis-aligned rectangles. Each rectangle is represented by a list of four integers: [x1, y1, x2, y2]
, where (x1, y1)
is the coordinate of the bottom-left corner, and (x2, y2)
is the coordinate of the top-right corner. The sides of the rectangles are parallel to the x and y axes.
Objective: Write a function that determines whether the two rectangles overlap. Two rectangles overlap if the area of their intersection is positive. Rectangles that only touch at a corner or edge are considered non-overlapping.
Examples:
Input: rec1 = [0, 0, 2, 2]
, rec2 = [1, 1, 3, 3]
Output: true
(The rectangles overlap as their intersection has a positive area.)
Input: rec1 = [0, 0, 1, 1]
, rec2 = [1, 0, 2, 1]
Output: false
(The rectangles touch along an edge but do not overlap.)
Input: rec1 = [0, 0, 1, 1]
, rec2 = [2, 2, 3, 3]
Output: false
(The rectangles are completely disjoint and do not overlap.)
Constraints:
x1 < x2
and y1 < y2
for each rectangle).When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To determine if two rectangles overlap using a brute force strategy, we essentially check all the ways they could possibly *not* overlap. If none of those non-overlapping scenarios are true, then the rectangles must overlap.
Here's how the algorithm would work step-by-step:
def is_rectangle_overlap(rectangle_one, rectangle_two):
# Check if rectangle one is to the left of rectangle two
if rectangle_one[2] <= rectangle_two[0]:
return False
# Check if rectangle one is to the right of rectangle two
if rectangle_one[0] >= rectangle_two[2]:
return False
# Check if rectangle one is above rectangle two
if rectangle_one[3] <= rectangle_two[1]:
return False
# Check if rectangle one is below rectangle two
if rectangle_one[1] >= rectangle_two[3]:
return False
# If none of the above conditions are true, then the rectangles overlap
return True
The core idea is to determine when rectangles *don't* overlap, which is easier to check. If none of these non-overlap conditions are met, then the rectangles must overlap. This avoids complicated geometric calculations.
Here's how the algorithm would work step-by-step:
def is_rectangle_overlap(rectangle_a, rectangle_b):
# Check if rectangle A is to the left of rectangle B
if rectangle_a[2] <= rectangle_b[0]:
return False
# Check if rectangle A is to the right of rectangle B
if rectangle_a[0] >= rectangle_b[2]:
return False
# Check if rectangle A is above rectangle B
if rectangle_a[3] <= rectangle_b[1]:
return False
# Check if rectangle A is below rectangle B
if rectangle_a[1] >= rectangle_b[3]:
return False
# If none of the above conditions are true, rectangles overlap
return True
Case | How to Handle |
---|---|
Null or empty input arrays (rec1 or rec2 is null or empty) | Return false immediately as an empty rectangle cannot overlap. |
Invalid rectangle: x1 >= x2 or y1 >= y2 for either rectangle | Return false because it represents an invalid or degenerate rectangle with no area. |
Rectangles are identical (same coordinates) | Return true as identical rectangles clearly overlap. |
Rectangles share a side (touching, but not overlapping) | Return false as touching sides does not constitute overlap. |
One rectangle is completely contained within the other | Return true as the inner rectangle is overlapping. |
Negative coordinates for rectangles | The overlap condition should correctly handle negative coordinates since it considers relative positions. |
Large coordinate values that could potentially cause integer overflow during calculations | Ensure calculations are performed using a data type large enough to accommodate the potential range of coordinate differences (e.g., long). |
Extreme coordinate values (close to the maximum/minimum integer values) | The overlap condition will still work correctly assuming no overflow occurs in the calculation. |