Skip to the code
cf.
+10 XP

#661 Overlap Area of Two Boxes

easy

A layout debugger highlights where two elements overlap. Each element's box is like the result of getBoundingClientRect(): { x, y, width, height }, where (x, y) is the top-left corner and y grows downward.

Write overlapArea(a, b) that returns the area of the region covered by both boxes. Boxes that only touch along an edge or a corner overlap with area 0.

Example 1

Input: a = {"x":0,"y":0,"width":100,"height":50}, b = {"x":60,"y":20,"width":80,"height":60}
Output: 1200

The shared region runs from x = 60 to 100 and from y = 20 to 50: 40 × 30 = 1200.

Example 2

Input: a = {"x":0,"y":0,"width":10,"height":10}, b = {"x":10,"y":0,"width":5,"height":5}
Output: 0

The boxes only touch along the line x = 10.

Constraints

  • -10^4 <= x, y <= 10^4
  • 0 <= width, height <= 10^4
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).