Intersection of Two Rectangles
Reported by candidates from Figma's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The detail that decides this Figma question is one line: rectangles that only touch along an edge or at a point return an empty array. Figma reported it in August 2024. It's an axis-aligned rectangle intersection, which is basically math on four numbers, no data structure needed. Candidates lose it on the touching case in Example 3, not on the overlap logic. If you blank on the boundary condition during the live OA, StealthCoder runs invisibly as a safety net and hands you the clean version. Most people won't need it, because the whole solution is about five lines.
The problem
Each axis-aligned rectangle is represented as [left, bottom, right, top], where left < right and bottom < top. Return the positive-area intersection of first and second in the same format. If they do not overlap with positive area, return an empty array. Rectangles that only touch along an edge or at one point have no positive-area intersection. Function intersectRectangles(first: int[], second: int[]) → int[] Examples Example 1 first = [0,0,4,4] second = [2,1,6,3] return = [2,1,4,3] The common horizontal interval is [2, 4], and the common vertical interval is [1, 3]. Example 2 first = [-3,-2,5,7] second = [0,0,2,2] return = [0,0,2,2] The second rectangle lies completely inside the first. Example 3 first = [0,0,2,2] second = [2,0,5,3] return = [] The rectangles touch at the vertical edge x = 2 but share no positive area. Constraints first.length == second.length == 4. -10^9 <= left < right <= 10^9. -10^9 <= bottom < top <= 10^9.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to treat each axis independently. The intersection's left is max(first left, second left). Its right is min(first right, second right). Do the same for bottom (max) and top (min). Then check that the new left is strictly less than the new right, and the new bottom is strictly less than the new top. If either check fails, return an empty array. The pitfall is using <= instead of <, which would return a zero-width rectangle for the touching case in Example 3. Another pitfall is returning null or [0,0,0,0] when the problem wants an empty array. Values go up to 10^9 in magnitude, and since you only compare and never add or multiply, overflow isn't a concern in most languages. If you freeze on the strictness check during the live OA, StealthCoder is the hedge that gives you the working code. Complexity is O(1) time and space.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Intersection of Two Rectangles cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Figma's OA.
Figma reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Intersection of Two Rectangles FAQ
What's the trick to the Figma rectangle intersection problem?+
Handle x and y separately. Take the max of the two lefts and the min of the two rights, then the max of the bottoms and the min of the tops. If the resulting left is not strictly less than right, or bottom not strictly less than top, there's no positive-area overlap.
How do I handle rectangles that only touch?+
Use strict inequality. After computing the candidate rectangle, require left < right and bottom < top. If left equals right, the width is zero, so return an empty array. Example 3 in the problem tests exactly this, with the shared edge at x = 2.
How hard is this problem really?+
Easy. It's constant time with no loops or data structures. The difficulty is only in the boundary condition and in returning the right format. Write it, then test the three given examples by hand, especially the touching case and the containment case.
Do I need to worry about integer overflow?+
Barely. Coordinates range from -10^9 to 10^9, which fits in a 32-bit signed integer. You only compare values and never add or multiply them, so you won't overflow. Don't compute areas or widths unless you use a 64-bit type.
How do I prepare for this in 48 hours?+
Write the function from scratch twice without looking. Then run your own edge cases: full containment, touching at an edge, touching at a corner, identical rectangles, and negative coordinates. Spend the remaining time on other OA patterns, since this one takes ten minutes.