Perfect Rectangle

HardMathhashing

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Each entry rectangles[i] = [x1, y1, x2, y2] is an axis-aligned rectangle with bottom-left corner (x1, y1) and top-right corner (x2, y2), where x1 < x2 and y1 < y2. Return true if the rectangles together tile one larger rectangle **exactly**: their union is a rectangle, with no gaps inside it and no area covered twice. Rectangles that only share an edge or a corner do not overlap. Otherwise return false. Aim for O(n) expected time.

Examples

Input: rectangles = [[1,1,3,3],[3,1,4,2],[3,2,4,4],[1,3,2,4],[2,3,3,4]]
Output: true
The five pieces fit together into the rectangle from (1,1) to (4,4) with nothing left over.
Input: rectangles = [[1,1,2,3],[1,3,2,4],[3,1,4,2],[3,2,4,4]]
Output: false
The column between x = 2 and x = 3 is never covered.
Input: rectangles = [[1,1,3,3],[3,1,4,2],[1,3,2,4],[2,2,4,4]]
Output: false
The last rectangle overlaps the first on the square from (2,2) to (3,3).

Constraints

  • 1 <= rectangles.length <= 2 * 10^4
  • -10^5 <= x1 < x2 <= 10^5
  • -10^5 <= y1 < y2 <= 10^5

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More hashing problems