Bounding Box Intersection over Union
Updated
Problem statement
Given two axis-aligned boxes boxA and boxB, return their intersection over union (IoU): the area where the two boxes overlap, divided by the total area that the two boxes cover together.
IoU is how object detection scores a predicted box against the true box. The closer it is to 1, the better the two boxes match, and 0 means they share no area.
Each box is a list of four integers, [x1, y1, x2, y2]: the points (x1, y1) and (x2, y2) are two opposite corners of the box. The corners may come in either order, so x1 may be larger than x2, and y1 may be larger than y2. Coordinates may be negative. The box covers every point from its smaller x to its larger x and from its smaller y to its larger y, so its width is |x2 - x1| and its height is |y2 - y1|.
Two boxes that meet only at an edge or a corner share no area, so their overlap is 0. A box may have zero width or zero height, which gives it an area of 0. If both boxes have an area of 0, the area covered is 0 too; return 0 in that case.
An answer within 10^-5 of the correct value is accepted.
Function
intersectionOverUnion(boxA: int[], boxB: int[]) → doubleExamples
Example 1
boxA = [0,0,4,3]boxB = [6,4,2,1]return = 0.2boxB lists its top-right corner first; it is the box from x = 2 to 6 and y = 1 to 4. The boxes overlap from x = 2 to 4 and y = 1 to 3, an area of 2 × 2 = 4. Each box has area 12, so the union is 12 + 12 − 4 = 20, and 4 / 20 = 0.2.
Example 2
boxA = [0,0,2,2]boxB = [2,0,4,2]return = 0.0The boxes share the edge x = 2 but no area, so the overlap is 0 and so is the IoU.
Example 3
boxA = [-3,-2,3,2]boxB = [1,1,-1,-1]return = 0.16666666666666666boxA is 6 by 4, area 24. boxB is the 2 by 2 square from (-1, -1) to (1, 1), which lies inside boxA. The overlap is all of boxB, 4, and the union is all of boxA, 24, so the IoU is 4 / 24 = 0.1666….
Constraints
boxA.length == boxB.length == 4-10^4 <= boxA[i], boxB[i] <= 10^4
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Rectangle OverlapMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Correct Three Protractor ReadingsSusquehanna International Group (SIG) · ONSITE INTERVIEWSusquehanna International Group (SIG) interview questions
- Overlap Area of Two RectanglesNutanix · ONSITE INTERVIEWNutanix interview questions
- Common Part of Two RectanglesExpedia · ONSITE INTERVIEWExpedia interview questions
- Max Points on a LineLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Rasterize a Circle with Integer PixelsPure Storage · ONSITE INTERVIEWPure Storage interview questions
- Point Inside a TriangleSambaNova Systems · PHONE SCREEN · ONSITE INTERVIEWSambaNova Systems interview questions
- Circle Intersection in Three DimensionsNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Range of a Third CorrelationTower Research Capital · PHONE SCREEN
- Three Points in a Semicircle: Monte Carlo EstimateSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions