Compute Two-Rectangle Coverage from Eight Coordinates

Quick Overview

Compute two-rectangle union area by exact clamped overlap and inclusion-exclusion, including degenerate rectangles.

Compute Two-Rectangle Coverage from Eight Coordinates

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Return the total area covered by two axis-aligned rectangles, counting overlap once. The first is specified by bottom-left `(ax1,ay1)` and top-right `(ax2,ay2)`, and the second by the corresponding b coordinates. ```cpp int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2); ``` Keep all eight scalar parameters in that order. ### Examples ```text Input: ax1=-2, ay1=-1, ax2=3, ay2=2, bx1=1, by1=0, bx2=5, by2=4 Output: 27 ``` The individual areas are 15 and 16; overlap area is 4. ```text Input: ax1=0, ay1=0, ax2=0, ay2=5, bx1=1, by1=1, bx2=3, by2=4 Output: 6 ``` ### Constraints - Every coordinate is between -10000 and 10000. - Within each rectangle, lower x/y coordinates are at most their corresponding upper coordinates. - Zero-width or zero-height rectangles are allowed by those inequalities.

Overview: Compute two-rectangle union area by exact clamped overlap and inclusion-exclusion, including degenerate rectangles.

Google logo
Google
Jul 18, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Return the total area covered by two axis-aligned rectangles, counting overlap once. The first is specified by bottom-left (ax1,ay1) and top-right (ax2,ay2), and the second by the corresponding b coordinates.

int computeArea(int ax1, int ay1, int ax2, int ay2,
                int bx1, int by1, int bx2, int by2);

Keep all eight scalar parameters in that order.

Examples

Input: ax1=-2, ay1=-1, ax2=3, ay2=2,
       bx1=1, by1=0, bx2=5, by2=4
Output: 27

The individual areas are 15 and 16; overlap area is 4.

Input: ax1=0, ay1=0, ax2=0, ay2=5,
       bx1=1, by1=1, bx2=3, by2=4
Output: 6

Constraints

  • Every coordinate is between -10000 and 10000.
  • Within each rectangle, lower x/y coordinates are at most their corresponding upper coordinates.
  • Zero-width or zero-height rectangles are allowed by those inequalities.