Quick 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.

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

  1. The corners of a box can come in any order. Put every box into a fixed min/max form before you measure anything.
  2. Run your overlap computation on two boxes that are separated on both axes and check that the intersection area comes out as 0.
  3. A pair whose union is 0 still counts toward n when you take the mean.

Mean IoU of Matched Bounding Boxes

mediumGoogleSoftware Engineer

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.

Examples

Example 1
Input
([[0, 0, 2, 2]], [[1, 1, 3, 3]])
Output
0.142857
Notes
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.
Example 2
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]])
Output
0.354167
Notes
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.

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.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def mean_iou(predicted: list[list[int]], ground_truth: list[list[int]]) -> float:
Sample call
mean_iou([[0, 0, 2, 2]], [[1, 1, 3, 3]])
Language
Case 1
Input
([[0, 0, 2, 2]], [[1, 1, 3, 3]])
Expected
0.142857
Case 2
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
0.354167

Browse More Questions

Show the approach

Approach

Handle the pairs one at a time. For each box take x_min = min(x1, x2), x_max = max(x1, x2) and the same on y, so the corner order no longer matters, and compute its area as (x_max - x_min) * (y_max - y_min). The overlap width is min(x_max of a, x_max of b) - max(x_min of a, x_min of b), and the overlap height is the same expression on y. The intersection area is width * height only when both are positive; a width or height of 0 or less means the boxes are apart or touch only along an edge or at a corner, so the intersection is 0. Each side is tested on its own because two negative sides would multiply into a positive area for boxes that are apart on both axes. The union is area_a + area_b - intersection. A pair with a positive union adds intersection / union to a running total, and a pair with union 0 (two empty boxes) adds 0.0. After the loop, divide the total by n, so every pair counts, including the ones with IoU 0, and round the mean to 6 decimals the way Python's round(value, 6) does.

Why it is correct: the min/max form gives each box its true extent, the clamped overlap sides describe the true overlapping rectangle (empty when the boxes are apart on either axis), and the union formula counts the shared area once. Each term is therefore the pair's IoU as defined, and the total divided by n is their mean.

Edge cases: corners in any order, zero-width or zero-height boxes, two empty boxes (union 0), boxes that share only an edge or a corner, containment in either direction, and negative coordinates. Every area is at most 4 x 10^8, which fits in a 32-bit signed integer. The Java, JavaScript and C++ references reproduce Python's rounding: Java rounds the exact BigDecimal value of the double half to even, and JavaScript and C++ format the double to 6 decimals and send the exact halfway values (the odd multiples of 1/128) to the even digit.

Time complexity:
O(n)
Space complexity:
O(1)