Mean IoU of Matched Bounding Boxes with Corner Canonicalization
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
An object detection model outputs axis-aligned bounding boxes, and you evaluate it against ground-truth boxes with intersection over union (IoU): the area where two boxes overlap divided by the area covered by at least one of them.
You are given two lists of equal length n. `predicted[i]` and `ground_truth[i]` form a matched pair. Compute the IoU of every pair and return the mean IoU over all n pairs.
### Function Signature
```python
def mean_iou(predicted: List[List[int]], ground_truth: List[List[int]]) -> float:
```
### Rules
- A box is `[x1, y1, x2, y2]`: two opposite corners of the rectangle. The corners can come in any order, so `[2, 2, 0, 0]`, `[0, 2, 2, 0]` and `[0, 0, 2, 2]` are the same box. Its extent is `x_min = min(x1, x2)`, `x_max = max(x1, x2)`, and likewise for y.
- The area of a box is `(x_max - x_min) * (y_max - y_min)`. A box with zero width or zero height has area 0.
- The intersection of two boxes is their overlapping rectangle. Boxes that are apart, or that share only an edge or a corner, have intersection area 0.
- For one pair, `union = area_a + area_b - intersection` and `IoU = intersection / union`. If `union` is 0 (both boxes have zero area), that pair's IoU is 0.0.
- Return the sum of the n per-pair IoU values divided by n, rounded with Python's `round(value, 6)`.
### Constraints
- 1 ≤ n ≤ 10^5, and `len(predicted) == len(ground_truth) == n`.
- Every coordinate is an integer in [-10^4, 10^4]; coordinates can be negative.
- Each box area is at most 4 × 10^8 and each union at most 8 × 10^8, so every intermediate area stays below 2^31 - 1.
```hint Try boxes far apart
Run your overlap computation on two boxes that are separated on both axes and check that the intersection area comes out as 0.
```
### Examples
Example 1
```
predicted = [[0, 0, 2, 2]]
ground_truth = [[1, 1, 3, 3]]
output = 0.142857
```
The overlap is the unit square from (1, 1) to (2, 2), area 1. The union is 4 + 4 - 1 = 7, so the IoU is 1/7.
Example 2
```
predicted = [[2, 2, 0, 0], [0, 0, 4, 4], [-3, -1, -1, 1]]
ground_truth = [[0, 0, 2, 2], [1, 1, 2, 2], [1, 1, 2, 2]]
output = 0.354167
```
Pair 1 is the same box after canonicalization (IoU 1.0). In pair 2 the ground-truth box lies inside the prediction: intersection 1, union 16, IoU 0.0625. Pair 3 does not overlap (IoU 0.0). The mean is 1.0625 / 3 = 0.3541666..., which rounds to 0.354167.
Example 3
```
predicted = [[0, 0, 1, 1], [5, 5, 5, 8]]
ground_truth = [[1, 0, 2, 1], [5, 5, 5, 8]]
output = 0.0
```
The boxes in pair 1 share only the edge x = 1, so their intersection is 0. Both boxes in pair 2 have zero width, so the union is 0 and the IoU is 0.0.
Overview: Compute the mean intersection over union between matched predicted and ground-truth bounding boxes whose corners can arrive in any order. Tests corner canonicalization, disjoint, touching and nested boxes, negative coordinates, zero-area boxes and averaging the per-pair scores.
Read the full Google Software Engineer interview experience this question came from
An object detection model outputs axis-aligned bounding boxes, and you evaluate it against ground-truth boxes with intersection over union (IoU): the area where two boxes overlap divided by the area covered by at least one of them.
You are given two lists of equal length n, `predicted` and `ground_truth`. `predicted[i]` and `ground_truth[i]` form a matched pair. Compute the IoU of every pair and return the mean IoU over all n pairs as a floating-point number.
Rules:
- A box is `[x1, y1, x2, y2]`: two opposite corners of the rectangle. The corners can come in any order, so `[2, 2, 0, 0]`, `[0, 2, 2, 0]` and `[0, 0, 2, 2]` are the same box. Its extent is `x_min = min(x1, x2)`, `x_max = max(x1, x2)`, and likewise for y.
- The area of a box is `(x_max - x_min) * (y_max - y_min)`. A box with zero width or zero height has area 0.
- The intersection of two boxes is their overlapping rectangle. Boxes that are apart, or that share only an edge or a corner, have intersection area 0.
- For one pair, `union = area_a + area_b - intersection` and `IoU = intersection / union`. If `union` is 0 (both boxes have zero area), that pair's IoU is 0.0.
- Return the sum of the n per-pair IoU values divided by n, rounded with Python's `round(value, 6)`. That function rounds the exact value of the double to 6 decimal places and sends an exact halfway value to the even digit.
Example 1:
Input: predicted = [[0, 0, 2, 2]], ground_truth = [[1, 1, 3, 3]]
Output: 0.142857
The overlap is the unit square from (1, 1) to (2, 2), area 1. The union is 4 + 4 - 1 = 7, so the IoU is 1/7.
Example 2:
Input: predicted = [[2, 2, 0, 0], [0, 0, 4, 4], [-3, -1, -1, 1]], ground_truth = [[0, 0, 2, 2], [1, 1, 2, 2], [1, 1, 2, 2]]
Output: 0.354167
Pair 1 is the same box once its corners are put in min/max order (IoU 1.0). In pair 2 the ground-truth box lies inside the prediction: intersection 1, union 16, IoU 0.0625. Pair 3 does not overlap (IoU 0.0). The mean is 1.0625 / 3 = 0.3541666..., which rounds to 0.354167.
Constraints:
- 1 <= n <= 10^5, and len(predicted) == len(ground_truth) == n.
- Each box is a list of exactly 4 integers [x1, y1, x2, y2].
- Every coordinate is an integer in [-10^4, 10^4]; coordinates can be negative.
- Each box area is at most 4 x 10^8 and each union at most 8 x 10^8, so every area, intersection and union stays below 2^31-1 and fits in a 32-bit signed integer.
Constraints
- 1 <= n <= 10^5, and len(predicted) == len(ground_truth) == n.
- Each box is a list of exactly 4 integers [x1, y1, x2, y2].
- Every coordinate is an integer in [-10^4, 10^4]; coordinates can be negative.
- Each box area is at most 4 x 10^8 and each union at most 8 x 10^8, so every area, intersection and union stays below 2^31-1 and fits in a 32-bit signed integer.
Examples
Input: ([[0, 0, 2, 2]], [[1, 1, 3, 3]])
Expected Output: 0.142857
Explanation: Source example 1: the overlap is the unit square from (1, 1) to (2, 2), the union is 4 + 4 - 1 = 7, and 1/7 = 0.1428571... rounds to 0.142857.
Input: ([[2, 2, 0, 0], [0, 0, 4, 4], [-3, -1, -1, 1]], [[0, 0, 2, 2], [1, 1, 2, 2], [1, 1, 2, 2]])
Expected Output: 0.354167
Explanation: Source example 2: IoU 1.0 for the reordered corners, 0.0625 for the contained box and 0.0 for the pair that is apart; 1.0625 / 3 = 0.3541666... rounds up to 0.354167.
Hints
- The corners of a box can come in any order. Put every box into a fixed min/max form before you measure anything.
- Run your overlap computation on two boxes that are separated on both axes and check that the intersection area comes out as 0.
- A pair whose union is 0 still counts toward n when you take the mean.