Largest axis-aligned rectangle from points
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Waymo's coding round, last asked Apr 2026. It tests hashing, geometry and arrays.
You are given a list of distinct points on a 2D integer grid. Each point is a pair [x, y].
Return the largest area of a rectangle whose sides are parallel to the x and y axes and whose four corners are all points in the list. Other points may lie inside the rectangle or on its edges. If no such rectangle exists, return 0.
Implement:
def largest_rectangle_area(points: list[list[int]]) -> int: ...
Constraints
0 <= len(points) <= 2000-10^9 <= x, y <= 10^9- Points are distinct.
Example 1
Input: [[0,0],[0,1],[1,0],[1,1],[4,0],[4,1]]
Output: 4
Three rectangles exist: x in [0,1] (area 1), x in [1,4] (area 3) and x in [0,4] (area 4). The largest is 4.
Example 2
Input: [[0,0],[1,1],[2,2],[3,0]]
Output: 0
No four points form an axis-aligned rectangle.
Start with a straightforward solution, then improve it. Be ready to discuss what changes if the rectangle must contain no other point.
Clarifying questions
- Does a zero-area rectangle count? (It cannot, with distinct points, but should we say so?)
- Is the area always an integer, and can it exceed 64 bits? (Up to about
4 * 10^18.) - Is O(n^2) fine for
n = 2000? - Should we return the corners, or only the area?
What a strong answer covers
- Starts with O(n^2): put the points in a hash set, try each pair as opposite corners with different
xandy, and check the other two corners. - Improves the constant: group points by
x, and for each pair ofyvalues in a column remember the lastxwhere that pair appeared. Two columns sharing a pair form a rectangle. - Explains why that is still correct for the largest area, not only the narrowest one: remember the first
xfor each pair, not only the last. - Tests fewer than four points, collinear points, several rectangles nested inside each other, and very large coordinates.
Follow-ups
- The rectangle must have no other point inside it or on its edges.
- Allow rotated rectangles: group pairs of points by midpoint and length.
- Return the smallest area instead, or count all rectangles.
- Points are added one at a time, and the answer is asked after each one.
Where it's asked
- Waymo · Coding · Last asked Apr 202645 min · Usually two rounds; some give you the interviewer's buggy code to find and fix, some are read aloud with no skeleton
What it tests
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
