Solve a Jigsaw Puzzle
Problem Description
You are provided with a collection of jigsaw puzzle pieces. Each piece has four distinct edges: top, right, bottom, and left. A helper function match(edgeA, edgeB) is available, which determines if two given edges can be connected, returning true or false.
The puzzle is guaranteed to form a single complete rectangular grid, but its dimensions (number of rows and columns) are unknown. Pieces may require rotation to fit correctly.
Implement a function solve(pieces) that returns a valid rectangular arrangement of all provided pieces. This arrangement must include the correct orientation for each piece, ensuring that every horizontally or vertically adjacent pair of edges matches according to the match function.
Assumptions and Constraints
- Every piece must be used exactly once.
- A valid solution is guaranteed to exist.
- The puzzle has no missing pieces.
- Edges on the border of the complete puzzle do not need to match anything external.
Discussion Points
Candidates should be prepared to discuss the algorithm, chosen data structures, and the time and space complexity of their solution.
AI interview prep
Core dilemma
Navigating a vast search space involving piece placement and rotation while dynamically determining the puzzle's dimensions.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Solve a Jigsaw Puzzle
Problem Description
You are provided with a collection of jigsaw puzzle pieces. Each piece has four distinct edges: top, right, bottom, and left. A helper function match(edgeA, edgeB) is available, which determines if two given edges can be connected, returning true or false.
The puzzle is guaranteed to form a single complete rectangular grid, but its dimensions (number of rows and columns) are unknown. Pieces may require rotation to fit correctly.
Implement a function solve(pieces) that returns a valid rectangular arrangement of all provided pieces. This arrangement must include the correct orientation for each piece, ensuring that every horizontally or vertically adjacent pair of edges matches according to the match function.
Assumptions and Constraints
- Every piece must be used exactly once.
- A valid solution is guaranteed to exist.
- The puzzle has no missing pieces.
- Edges on the border of the complete puzzle do not need to match anything external.
Discussion Points
Candidates should be prepared to discuss the algorithm, chosen data structures, and the time and space complexity of their solution.