Skip to content
DeepForgeby svx
All problems
EasyAlgorithmsal-218

Rectangle Overlap Area

Problem

Given two axis-aligned rectangles by their corners (ax1, ay1, ax2, ay2) and (bx1, by1, bx2, by2), return the area of their intersection, or 0 when they do not overlap.

The overlap width is the smaller right edge minus the larger left edge, and likewise for the height. Rectangles that only touch along an edge have zero overlap area.

Hint

Clamp the overlap to zero whenever the computed width or height is not positive.

Test cases

case 1
in: [0,0,2,2,1,1,3,3]
expected: 1
case 2
in: [0,0,1,1,2,2,3,3]
expected: 0
case 3
in: [0,0,2,2,0,0,2,2]
expected: 4
case 4
in: [0,0,1,1,1,1,2,2]
expected: 0
case 5
in: [-1,-1,1,1,0,0,2,2]
expected: 1
Starter codePython
def rectangle_overlap(ax1, ay1, ax2, ay2, bx1, by1, bx2, by2):
    # Your code here
    pass

5 test cases · run your code against them in the editor