Design a Jigsaw Puzzle Solver Class with Edge Fit Checks and Border Pieces
Company: Asana
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Other
Design a jigsaw puzzle solver that models puzzle pieces and determines whether two pieces can fit together along a specified side.
Each piece has exactly four edges (`top`, `right`, `bottom`, `left`). Each edge is one of three shapes: `FLAT`, `TAB` (a protruding bump), or `BLANK` (a matching indentation). Two edges fit together if and only if one is a `TAB` and the other is a `BLANK`. Two `FLAT` edges, or any pair of the same shape, do not fit.
Pieces are identified by a unique integer `id`. When checking a fit between piece `A` and piece `B` on side `s` of `A`, the matching side on `B` is the opposite side (for example, `A`'s `right` must match `B`'s `left`). The solver also tracks how many pieces have been added and can return the ids of all pieces that have at least one `FLAT` edge (the border pieces), sorted in ascending order.
Implement the following methods:
- `addPiece(id: int, top: str, right: str, bottom: str, left: str)`: add a piece with the given id and four edge shapes (`"FLAT"`, `"TAB"`, or `"BLANK"`).
- `canFit(idA: int, sideA: str, idB: int) -> bool`: return `True` if side `sideA` of piece `idA` fits against the opposite side of piece `idB` (one must be `TAB`, the other `BLANK`). Return `False` if either piece does not exist or the edges do not match.
- `getPiece(id: int) -> list`: return the piece's edges as `[top, right, bottom, left]`, or an empty list if the piece does not exist.
- `count() -> int`: return the total number of pieces currently stored.
- `getBorderPieces() -> list`: return a sorted list of the ids of all pieces that have at least one `FLAT` edge.
For example:
```text
solver = JigsawPuzzleSolver()
solver.addPiece(5, "FLAT", "TAB", "BLANK", "FLAT")
solver.addPiece(2, "FLAT", "FLAT", "TAB", "BLANK")
solver.addPiece(3, "BLANK", "TAB", "TAB", "BLANK")
solver.canFit(5, "right", 2) -> True (5.right is TAB, 2.left is BLANK)
solver.canFit(2, "left", 5) -> True (2.left is BLANK, 5.right is TAB)
solver.canFit(2, "bottom", 3) -> True (2.bottom is TAB, 3.top is BLANK)
solver.canFit(5, "bottom", 3) -> False (5.bottom is BLANK, 3.top is BLANK)
solver.canFit(5, "top", 2) -> False (5.top is FLAT, 2.bottom is TAB)
solver.canFit(5, "right", 99) -> False (piece 99 does not exist)
solver.getPiece(3) -> ["BLANK", "TAB", "TAB", "BLANK"]
solver.getPiece(7) -> []
solver.count() -> 3
solver.getBorderPieces() -> [2, 5]
```
Piece 3 has no `FLAT` edge, so it is not a border piece. The border list comes back in ascending order even though piece 5 was added before piece 2.
```hint Where the work goes
Pick a representation that makes the opposite side a fixed lookup, and think about what `addPiece` can record so that `getBorderPieces` never has to scan every stored piece.
```
### Constraints & Assumptions
- `1 <= id <= 10^5`
- Edge shapes are one of `"FLAT"`, `"TAB"`, `"BLANK"`.
- Sides are one of `"top"`, `"right"`, `"bottom"`, `"left"`.
- At most `10^4` calls are made across all methods.
- All piece ids passed to `addPiece` are unique.
- `canFit` and `getPiece` may be called with ids that do not exist and must handle them without failing.
### Clarifying Questions to Ask
- Can `canFit` be called with the same id for both pieces, and should a piece ever fit against itself?
- Is each piece's orientation fixed as given, or may a piece be rotated before it is checked?
- For input outside the stated values, such as an unknown side name or edge shape, should the method return `False` or raise an error?
### What a Strong Answer Covers
- A storage choice that gives constant-time lookup by id and a fixed edge order for `getPiece`.
- The mapping from each side to its opposite, and a fit test that accepts exactly one `TAB` with one `BLANK` in either order.
- Correct results for ids that do not exist, without exceptions.
- Border pieces tracked at insert time and returned sorted, with the time and space cost of each method stated.
### Follow-up Questions
- How would `canFit` change if piece `B` may be rotated by 90, 180 or 270 degrees before it is placed?
- In a real puzzle a tab fits only one particular blank. How would you model edge shapes so you can find the partner of a given edge quickly?
- If `getBorderPieces` is called far more often than `addPiece`, how would you avoid sorting on every call?
- How would you build on this class to assemble the full rectangular grid?
Overview: Design a class that stores jigsaw puzzle pieces with four edge shapes, checks whether two pieces fit along a given side, and lists the border pieces in ascending order. It tests hash map modeling, the opposite-side mapping, graceful handling of missing ids, and what to precompute at insert time.