FastPrepUnion Area of Two Rectangles

Union Area of Two Rectangles

DocuSign logoDocuSign● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

Two axis-aligned rectangles are described by their bottom-left and top-right corners: (ax1, ay1), (ax2, ay2) and (bx1, by1), (bx2, by2).

Return the total area covered by at least one rectangle.

Function

rectangleUnionArea(ax1: int, ay1: int, ax2: int, ay2: int, bx1: int, by1: int, bx2: int, by2: int) → long

Examples

Example 1

ax1 = -3ay1 = 0ax2 = 3ay2 = 4bx1 = 0by1 = -1bx2 = 9by2 = 2return = 45

The rectangle areas are 24 and 27, and their overlap area is 6, giving 45.

Example 2

ax1 = 0ay1 = 0ax2 = 2ay2 = 2bx1 = 2by1 = 0bx2 = 4by2 = 2return = 8

The rectangles touch at an edge but have zero overlap area.

Constraints

  • -10^9 <= ax1 < ax2 <= 10^9 and -10^9 <= ay1 < ay2 <= 10^9.
  • The same strict corner ordering holds for rectangle B.
  • The answer fits in a signed 64-bit integer.

More DocuSign problems

See DocuSign hiring insights
public long rectangleUnionArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
    // Write your code here.
}
ax1-3
ay10
ax23
ay24
bx10
by1-1
bx29
by22
expected45
Checking account…