Query Box Inside Every Saved Rectangle
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The edge case that breaks a naive solution in this ZipRecruiter OA, reported October 2022, is the empty state: a query shows up before any rectangle is saved, and the answer is true. Miss that and your first test fails. The problem looks like a geometry loop, but it's really a running-aggregate trick. You process up to 100000 operations, so rechecking every saved rectangle per query will time out. If you blank on the aggregate idea during the live OA, StealthCoder runs invisibly on your desktop as a safety net and hands you the approach.
The problem
Process operations: [0,a,b] saves an a by b rectangle. [1,a,b] asks whether the query box can fit inside every saved rectangle separately. Allow 90-degree rotation. Return query booleans in order; with no saved rectangles, return true. Function queryBoxesFitSavedRectangles(operations: int[][]) → boolean[] Examples Example 1 operations = [[0,3,3],[0,5,2],[1,3,2],[1,2,4]] return = [true,false] 3x2 fits both saved rectangles after rotation choices; 2x4 cannot fit inside 3x3. Example 2 operations = [[1,100,100]] return = [true] No rectangles have been saved. Constraints 1 <= operations.length <= 100000 All side lengths are positive.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: with rotation allowed, normalize every rectangle to (small, large) by sorting its two sides. A query box, also normalized to (qs, ql), fits inside a saved rectangle (s, l) exactly when qs <= s and ql <= l. To fit inside every saved rectangle, it must satisfy that against all of them. So you only track two values: the minimum small side and the minimum large side across all saves. Each save updates both minimums in O(1). Each query compares against them in O(1). Total work is O(n). The pitfall is skipping normalization and comparing a to a, b to b, which fails after rotation. Another pitfall is storing all rectangles and looping per query. Initialize the minimums to infinity so an empty set returns true naturally. StealthCoder is the hedge if the aggregate idea doesn't come to you under the clock.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Query Box Inside Every Saved Rectangle 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 by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Query Box Inside Every Saved Rectangle FAQ
What's the actual trick in the ZipRecruiter query box problem?+
Normalize every rectangle so the smaller side comes first. A query fits in all saved rectangles only if its small side is at most the minimum small side and its large side is at most the minimum large side. Keep two running minimums and answer each query in constant time.
How do I handle a query before any rectangle is saved?+
Start both minimums at infinity. Any query box then passes the comparison, so you return true with no special case. Example 2 tests this directly with a single query of 100 by 100 and no saves.
Why does rotation not need a brute-force check?+
Sorting each pair of sides makes rotation irrelevant. Comparing small to small and large to large is both necessary and sufficient for a box to fit in a rectangle. Trying both orientations per rectangle is redundant once everything is normalized.
Will an O(n) per query approach pass with 100000 operations?+
Probably not. Worst case is around 50000 saves and 50000 queries, which is billions of comparisons. The running-minimum approach is O(1) per operation, so the whole thing runs in linear time and avoids the timeout.
How do I prep for this in 48 hours?+
Write the solution once from scratch and test it on both examples. Then add your own cases: a query that fits only after rotation, a save made after a query, and an empty start. It's a short problem, so the edge cases are where you'll lose points.