FastPrepBounding Box Intersection over Union
Problems/Bounding Box Intersection over Union

Bounding Box Intersection over Union

Google logoGoogle● EasyINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported as the first of two video technical rounds, held one right after the other, in an interview for a software engineering internship for PhD students. The task was intersection over union for object detection: from the coordinates of the boxes, find the overlap area and the union area, and use their ratio to judge how well a model's boxes fit. The candidate says they talked through boxes that do not overlap, a box that lies fully inside the other, coordinates of either sign, and putting each box's corners into one standard order first. Near the end, the interviewer pointed out a missed step: a sum had to be scaled down, for example divided by a count. The report does not say which sum, so that step is not part of this problem; one likely reading is averaging the score over many boxes, which the editorial covers as a follow-up.

The report gives no interface, no examples and no limits, so these choices are ours: the function takes exactly two boxes and returns one number; each box is four integers, two opposite corners in either order; width and height are the plain differences of the coordinates, as for shapes on a plane, not pixel counts that add 1; boxes that meet only at an edge or a corner score 0; boxes with zero width or height are allowed. The report does not cover two boxes that both have zero area, where the union is 0 and the ratio is undefined; we assume the answer is 0 then, since there is nothing to match. Coordinates run from -10^4 to 10^4, an answer within 10^-5 is accepted, and all the examples are ours. The 2D range-sum question asked later in the same round and the second round are separate problems.

Our version is about 80% the same as the reported question.

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[]) → double

Examples

Example 1

boxA = [0,0,4,3]boxB = [6,4,2,1]return = 0.2

boxB 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.0

The 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.16666666666666666

boxA 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

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public double intersectionOverUnion(int[] boxA, int[] boxB) {
        // Write your code here.
    }
}
Checking account…