Rectangle Area

MediumMathmath

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Two axis-aligned rectangles are drawn on a 2D plane. Rectangle A has bottom-left corner (ax1, ay1) and top-right corner (ax2, ay2); rectangle B has bottom-left corner (bx1, by1) and top-right corner (bx2, by2). Return the total area covered by at least one of the two rectangles. Area covered by both is counted once. Rectangles that only share an edge or a corner have no overlapping area. A rectangle may be degenerate (zero width or height), in which case it contributes no area.

Examples

Input: ax1 = -3, ay1 = 0, ax2 = 3, ay2 = 4, bx1 = 0, by1 = -1, bx2 = 9, by2 = 2
Output: 45
A has area 24, B has area 27, and they overlap on a 3 x 2 region: 24 + 27 - 6 = 45.
Input: ax1 = -2, ay1 = -2, ax2 = 2, ay2 = 2, bx1 = -2, by1 = -2, bx2 = 2, by2 = 2
Output: 16
Both rectangles are the same 4 x 4 square.

Constraints

  • -10^4 <= ax1 <= ax2 <= 10^4
  • -10^4 <= ay1 <= ay2 <= 10^4
  • -10^4 <= bx1 <= bx2 <= 10^4
  • -10^4 <= by1 <= by2 <= 10^4

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More math problems