Back to Practice
#0023

Bounding Box IoU

MediumML18 min15 XP

Problem

Given box_a and box_b as (x1, y1, x2, y2), return intersection area divided by union area.

Why This Matters

IoU is the core overlap metric behind object detection evaluation and non-maximum suppression.

Function Signature

def box_iou(box_a, box_b):

Examples

Example 1
Inputbox A (0,0,2,2), box B (1,1,3,3)
Output1/7

Intersection area is 1. Each box area is 4. Union is 4 + 4 - 1 = 7.

Constraints

  • Boxes use (x1, y1, x2, y2).
  • Return 0 when boxes do not overlap.
  • Assume x2 >= x1 and y2 >= y1.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 4 hidden categories
Partial overlap
Input(0,0,2,2), (1,1,3,3)
Expected1/7

Only a 1 by 1 square overlaps.

No overlap
Input(0,0,1,1), (2,2,3,3)
Expected0

Intersection width and height are clamped to zero.

Hidden Test Categories
Identical boxesOne box inside anotherTouching edges onlyZero-area box