Rectangle Fit Queries
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
ZipRecruiter reportedly asked this one in September 2023, and it looks like a design problem until you see what it really is. Saved rectangles, box queries, rotation allowed. Strip the story and you're tracking two running numbers. If you've got an OA coming up, this is a ten-minute problem once the trick clicks. If you blank under the clock, StealthCoder is the invisible safety net that reads the problem on screen and hands you the solution.
The problem
For this exercise, use the callable contract below. Process the rows of operations from left to right. Each row has one of two forms: [0, a, b]: create and save a rectangle of size a × b. [1, a, b]: determine whether every rectangle saved by earlier operations can fit inside a box of size a × b. Test each saved rectangle separately; the rectangles do not need to fit in the box at the same time. You may rotate a rectangle by 90 degrees. Return one boolean for each query operation, in query order. Function solution(operations: int[][]) → boolean[] Examples Example 1 operations = [[1,1,1]] return = [true] No rectangles have been saved, so every saved rectangle vacuously fits and the answer is true. Example 2 operations = [[0,1,3],[0,4,2],[1,3,4],[1,3,2]] return = [true,false] Both saved rectangles fit the 3 × 4 box after choosing the appropriate orientation. The 4 × 2 rectangle cannot fit the later 3 × 2 box, so the answers are [true,false]. Constraints operations contains at least one row. operations[i].length = 3. operations[i][0] is either 0 or 1. operations[i][1] and operations[i][2] are positive integers.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Normalize every rectangle so its sides are (min, max). A rectangle fits a box if, after normalizing the box the same way, rect.min <= box.min and rect.max <= box.max. Rotation is already handled by sorting the sides. Now the key: you don't need to store rectangles. Keep maxMin, the largest of all the smaller sides, and maxMax, the largest of all the larger sides. A query passes only if box.min >= maxMin and box.max >= maxMax. Every rectangle fits separately, so the two maxima fully decide it. With no saves, both start at 0 and the answer is true. The common pitfall is looping over every saved rectangle per query, which turns O(n) into O(n^2). Another is forgetting to normalize the box. Each operation is O(1). StealthCoder is the hedge if the live OA makes you second-guess the reduction.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Rectangle Fit Queries 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Rectangle Fit Queries FAQ
What's the trick in Rectangle Fit Queries?+
Normalize each rectangle to (smaller, larger) sides and track only the max of the smaller sides and the max of the larger sides. A query box, also normalized, fits everything if both its sides meet those two maxima. No list of rectangles needed.
How do I handle rotation?+
Sort each pair of sides so the smaller comes first. Do it for saved rectangles and for query boxes. Once everything is normalized, rotation is baked in and you just compare smaller to smaller and larger to larger.
What should I return when no rectangles are saved?+
True. The first example shows it: a query on an empty set is vacuously true. Initialize both running maxima to 0 and the comparison handles it automatically, since positive box sides are always at least 0.
What's the time complexity I should aim for?+
O(1) per operation and O(n) overall for n operations, with O(1) extra space besides the output. If your solution scans all saved rectangles on each query, it works on the examples but is the slower approach and risks failing larger hidden tests.
How do I prep for this in 48 hours?+
Practice the pattern of replacing stored data with running aggregates. Write this solution once from memory, then test the empty case, a square, and a query where only one dimension fails. Those three cases catch most mistakes on this kind of problem.