Fewest Slides to Solve a Two by Three Tile Puzzle

Last updated: November 29, 2026

Quick Overview

A 2x3 board holds tiles 1 through 5 and one blank (0); a move slides a tile adjacent to the blank into it. Return the minimum number of moves to reach the solved board [[1,2,3],[4,5,0]], or -1 if it is unreachable.

Airbnb
Coding & Algorithms
Software Engineer
Airbnb
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


A 2x3 board holds tiles 1 through 5 and one blank (0); a move slides a tile adjacent to the blank into it. Return the minimum number of moves to reach the solved board [[1,2,3],[4,5,0]], or -1 if it is unreachable.

Airbnb onsites have long used small puzzles like this to see whether a candidate recognizes a state-space search hiding behind a game. The interviewer looks for BFS over encoded states, a precomputed neighbor table, and a sound argument for when the answer is -1.

What the Interviewer Expects
  • Model each board as a node and each slide as an unweighted edge
  • Encode the board as a string or integer to hash it
  • Use BFS so the first time the goal is seen is the shortest path
  • Precompute neighbor indexes for the flattened grid
  • Explain the parity reason some boards are unsolvable
Key Topics to Cover
BFS
state space search
hashing
permutation parity
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you return the actual sequence of moves?
  • How does this scale to a 3x3 or 4x4 board, and when would you switch to A* with Manhattan distance?
  • Can you decide solvability without searching?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input is a 2x3 board containing each of 0..5 once. Output is the minimum number of slides to reach 123450, or -1. Each move swaps the blank with an orthogonal neighbor....

High-Level Design

There are only 6! = 720 possible boards, so a full BFS is cheap. Flatten the board to a 6-character string so states are hashable. In the flattened layout, index i has fixed neighbors (for example ind...


Submit Your Answer
Markdown supported