Design a structure that checks whether a rectangle fits inside every stored rectangle
Company: SIG (Susquehanna)
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Other
Design a data structure that stores rectangles and supports checking whether a new rectangle fits inside every previously stored rectangle.
A candidate rectangle with dimensions `(w, h)` fits inside a stored rectangle `(W, H)` if either `w <= W and h <= H`, or, after a 90-degree rotation, `h <= W and w <= H`. Equivalently, it fits if `min(w, h) <= min(W, H)` and `max(w, h) <= max(W, H)`.
If no rectangles have been stored yet, `canFit` should return `true`.
Implement the following methods:
- `addRectangle(width: int, height: int)`: store a rectangle with the given width and height.
- `canFit(width: int, height: int) -> bool`: return `true` if a rectangle with the given dimensions fits (optionally rotated 90 degrees) inside every previously stored rectangle, otherwise `false`. Returns `true` when no rectangles have been stored.
Example 1:
```text
Input:
["RectangleFitChecker", "canFit", "addRectangle", "canFit", "canFit", "addRectangle", "canFit", "canFit"]
[[], [5, 5], [10, 8], [8, 10], [9, 9], [6, 12], [7, 5], [6, 11]]
Output: [null, true, null, true, false, null, true, false]
Explanation:
checker = RectangleFitChecker() # no rectangles stored
checker.canFit(5, 5) # true (no rectangles stored yet)
checker.addRectangle(10, 8) # stored mins: short=8, long=10
checker.canFit(8, 10) # true (fits 10x8 exactly after rotation)
checker.canFit(9, 9) # false (9 > 8, the short side of 10x8)
checker.addRectangle(6, 12) # stored mins: short=6, long=10
checker.canFit(7, 5) # true (normalized (5, 7): 5 <= 6 and 7 <= 10)
checker.canFit(6, 11) # false (normalized (6, 11): fits 6x12, but 11 > 10, the long side of 10x8)
```
Example 2:
```text
Input:
["RectangleFitChecker", "addRectangle", "addRectangle", "canFit", "canFit", "canFit", "canFit"]
[[], [4, 6], [5, 5], [4, 4], [5, 4], [4, 5], [1, 6]]
Output: [null, null, null, true, true, true, false]
Explanation:
checker = RectangleFitChecker() # no rectangles stored
checker.addRectangle(4, 6) # stored mins: short=4, long=6
checker.addRectangle(5, 5) # stored mins: short=4, long=5
checker.canFit(4, 4) # true (4 <= 4 and 4 <= 5)
checker.canFit(5, 4) # true (normalized (4, 5): 4 <= 4 and 5 <= 5)
checker.canFit(4, 5) # true (4 <= 4 and 5 <= 5)
checker.canFit(1, 6) # false (long side 6 > 5)
```
```hint Canonical form
Since rotation is allowed, write every rectangle with its sides in a fixed order. Then look at what "fits inside every stored rectangle" demands of each side separately.
```
### Constraints & Assumptions
- `1 <= width, height <= 10^9`
- At most `10^5` total calls to `addRectangle` and `canFit`.
- Fitting is axis-aligned: only the two orientations above count, and a side may equal the stored side.
- If no rectangles have been added, `canFit` returns `true`.
### What a Strong Answer Covers
- How rotation is handled, with a short argument that the min/max test matches the two-orientation definition.
- What state the structure keeps so that "fits every stored rectangle" does not require looking at each stored rectangle.
- The empty case and the choice of initial values.
- Time and space per operation, and why scanning all stored rectangles is too slow at the call limit.
### Follow-up Questions
- How would you support removing a stored rectangle?
- How would you answer whether the candidate fits inside at least one stored rectangle?
- What changes if a rectangle may be placed at any angle?
Overview: Design a data structure that stores rectangles and answers whether a new rectangle, rotated 90 degrees if needed, fits inside every rectangle stored so far. It tests handling rotation cleanly, reasoning about what the condition requires of each side, and keeping both operations fast across many calls.