Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Move from brute-force thinking to an efficient approach using math strategy.
Given the coordinates of two rectilinear rectangles in a 2D plane, return the total area covered by the two rectangles.
The first rectangle is defined by its bottom-left corner (ax1, ay1) and its top-right corner (ax2, ay2).
The second rectangle is defined by its bottom-left corner (bx1, by1) and its top-right corner (bx2, by2).
Example 1:
Input: ax1 = -3, ay1 = 0, ax2 = 3, ay2 = 4, bx1 = 0, by1 = -1, bx2 = 9, by2 = 2 Output: 45
Example 2:
Input: ax1 = -2, ay1 = -2, ax2 = 2, ay2 = 2, bx1 = -2, by1 = -2, bx2 = 2, by2 = 2 Output: 16
Constraints:
-104 <= ax1 <= ax2 <= 104-104 <= ay1 <= ay2 <= 104-104 <= bx1 <= bx2 <= 104-104 <= by1 <= by2 <= 104Problem summary: Given the coordinates of two rectilinear rectangles in a 2D plane, return the total area covered by the two rectangles. The first rectangle is defined by its bottom-left corner (ax1, ay1) and its top-right corner (ax2, ay2). The second rectangle is defined by its bottom-left corner (bx1, by1) and its top-right corner (bx2, by2).
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
-3 0 3 4 0 -1 9 2
-2 -2 2 2 -2 -2 2 2
rectangle-overlap)find-the-number-of-ways-to-place-people-ii)find-the-number-of-ways-to-place-people-i)find-the-largest-area-of-square-inside-two-rectangles)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #223: Rectangle Area
class Solution {
public int computeArea(int ax1, int ay1, int ax2, int ay2, int bx1, int by1, int bx2, int by2) {
int a = (ax2 - ax1) * (ay2 - ay1);
int b = (bx2 - bx1) * (by2 - by1);
int width = Math.min(ax2, bx2) - Math.max(ax1, bx1);
int height = Math.min(ay2, by2) - Math.max(ay1, by1);
return a + b - Math.max(height, 0) * Math.max(width, 0);
}
}
// Accepted solution for LeetCode #223: Rectangle Area
func computeArea(ax1 int, ay1 int, ax2 int, ay2 int, bx1 int, by1 int, bx2 int, by2 int) int {
a := (ax2 - ax1) * (ay2 - ay1)
b := (bx2 - bx1) * (by2 - by1)
width := min(ax2, bx2) - max(ax1, bx1)
height := min(ay2, by2) - max(ay1, by1)
return a + b - max(height, 0)*max(width, 0)
}
# Accepted solution for LeetCode #223: Rectangle Area
class Solution:
def computeArea(
self,
ax1: int,
ay1: int,
ax2: int,
ay2: int,
bx1: int,
by1: int,
bx2: int,
by2: int,
) -> int:
a = (ax2 - ax1) * (ay2 - ay1)
b = (bx2 - bx1) * (by2 - by1)
width = min(ax2, bx2) - max(ax1, bx1)
height = min(ay2, by2) - max(ay1, by1)
return a + b - max(height, 0) * max(width, 0)
// Accepted solution for LeetCode #223: Rectangle Area
struct Solution;
impl Solution {
fn compute_area(a: i32, b: i32, c: i32, d: i32, e: i32, f: i32, g: i32, h: i32) -> i32 {
let left = a.max(e);
let right = c.min(g);
let bottom = b.max(f);
let top = d.min(h);
let r1 = (c - a) * (d - b);
let r2 = (g - e) * (h - f);
let r3 = (right.max(left) - left) * (top.max(bottom) - bottom);
r1 + r2 - r3
}
}
#[test]
fn test() {
let a = -3;
let b = 0;
let c = 3;
let d = 4;
let e = 0;
let f = -1;
let g = 9;
let h = 2;
let res = 45;
assert_eq!(Solution::compute_area(a, b, c, d, e, f, g, h), res);
}
// Accepted solution for LeetCode #223: Rectangle Area
function computeArea(
ax1: number,
ay1: number,
ax2: number,
ay2: number,
bx1: number,
by1: number,
bx2: number,
by2: number,
): number {
const a = (ax2 - ax1) * (ay2 - ay1);
const b = (bx2 - bx1) * (by2 - by1);
const width = Math.min(ax2, bx2) - Math.max(ax1, bx1);
const height = Math.min(ay2, by2) - Math.max(ay1, by1);
return a + b - Math.max(width, 0) * Math.max(height, 0);
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.