skip to content

Do you have to build a sliding-tile puzzle's state graph before you can search it?

level: middleimportance: should knowfreq 55%

answer

  1. What exactly is one node here?
  2. An edge is one legal move
  3. Compare space size to explored region
  4. Start, successor rule, goal test
  5. A 4-by-4 board holds about 10^13 arrangements

basics

~20 s

No. The graph is defined by a move rule, not by stored data: a node is a whole board arrangement, an edge is one legal tile slide. A search generates moves on demand and touches only the arrangements it actually reaches.

solid answer

~50 s

A sliding-tile board is a state-space graph. Each node is a complete board arrangement, and each edge is one legal slide of a tile into the blank — two to four of them, depending on whether the blank sits in a corner, on an edge or in the middle. Nothing needs building: the search starts from the given arrangement and asks a successor routine for the arrangements one move away. That matters because the space is enormous. A 3-by-3 board has 9! = 362,880 arrangements, of which exactly half — 181,440 — are reachable from any given start, because a parity invariant splits the space into two components. Step up to 4-by-4 and it is about 10^13 arrangements: materializing is not merely wasteful, it is impossible. The search visits only the frontier it grows, which for a nearby goal may be a few thousand nodes.

go deeper

for a junior

Be ready to name the node and the edge for a puzzle with no visible network in it: the node is the whole arrangement, the edge is one legal move. That naming step is what the question is really testing.

for a middle

Explain that a start state, a successor rule and a goal test fully define the graph, and quantify why building it is hopeless: half of 9! arrangements on a small board, about 10^13 on the next size up.

for a senior

Show the decision, not the slogan. State the conditions under which precomputing a small fixed space beats generating, and mention what you lose without a node list, such as any algorithm that iterates all nodes.

for a principal

Own the boundary between the model and the search. Keep the successor rule the single place business constraints live, so a changed rule cannot leave one team's precomputed copy of the space silently stale.

## The state space is a graph you never see The hardest step in a modeling interview is not the traversal — it is recognizing that a problem with no obvious network in it is still a graph. The recognition test has three parts: - Is there a **situation** you can describe completely and compare for equality? That is a node. - Is there an **atomic legal action** that turns one situation into another? That is an edge. - Is there a **target situation**, or a property a situation must have? That is the goal test. A sliding-tile board passes all three. The situation is the full arrangement of tiles including where the blank sits. The action is sliding one tile into the blank — equivalently, moving the blank one square. The target is the ordered arrangement. That is a graph, and everything a graph algorithm can do applies to it, without a single stored edge. ## Why nobody builds it The branching factor is small: the blank has 2 legal moves from a corner, 3 from an edge square, 4 from the middle. But the node count is brutal. A 3-by-3 board has 9! = 362,880 arrangements. From any given start, exactly 181,440 of them are reachable — the permutation parity of the tiles combined with the blank's position is invariant under legal moves, so the space splits into two components that never meet. This is why a scrambled puzzle can be provably unsolvable rather than merely hard. A 4-by-4 board has 16!/2 ≈ 10^13 reachable arrangements. No machine builds that. Yet a search from a start eight moves from the goal expands only a few thousand nodes. That gap — between the size of the space and the size of the explored region — is the entire argument for implicit graphs. **Materializing costs you the whole space; generating on demand costs you only what you touch.** ## The successor routine is the graph In place of stored adjacency you carry three things: a start state, a successor routine, and a goal test. Anything else that a stored graph would give you has to be recovered another way. Two consequences worth stating in an interview: - **You cannot enumerate all nodes cheaply.** Algorithms that begin "for every node in the graph" — computing a global property, iterating to find unvisited components — do not transfer. Implicit-graph work is reachability work from a known start. - **You cannot cheaply ask "is X adjacent to Y?"** in general; you generate X's neighbors and look. For most searches that is all you ever needed. ## When materializing does pay The on-demand answer is not universal, and a good candidate says where it flips. Consider a four-wheel dial mechanism where a reading is four digits and one click of one wheel is an edge. That space is exactly 10^4 = 10,000 readings, each with 8 neighbors — one click up or down on each of four wheels, wrapping at the ends. Building it is trivial and finishes in milliseconds. If a service answers thousands of queries a day against the same fixed dial and the same fixed set of unusable readings, one build amortizes across all of them and every query becomes a lookup. For a single one-off query, the generator is simpler, allocates nothing, and touches only the readings the search actually reaches. The decision rule that falls out: **materialize when the space is small, fixed, and traversed repeatedly; generate when it is large, sparsely explored, or defined by rules that change per request.** The size of the state space relative to the explored region is the number that decides it, not taste. ## Where unusable states live If certain arrangements are forbidden — a jammed wheel position, a shelf under maintenance — the natural home is the successor routine: it simply does not emit them, so they are not nodes. Seeding them into the visited set instead is a common shortcut that usually behaves identically, but it conflates "cannot exist" with "already explored", and it has one real bug: if the start itself is forbidden, the shortcut quietly reports no solution for the wrong reason. ## The sentence to have ready "A node is a whole board arrangement, an edge is one legal slide, and the graph is the move rule — so I search from the start and generate neighbors as I go, because the space is 181,440 reachable arrangements on a 3-by-3 board and about 10^13 on a 4-by-4." That answer shows the modeling, the cost awareness, and the reason for the choice, in one breath.

  • What would make you precompute the whole state space instead of generating neighbors?
    A space that is small, fixed and queried repeatedly. If the mechanism has ten thousand states and the service answers thousands of queries a day against the same rules, one build amortizes and each query becomes a lookup. For a huge space, a one-off query, or rules that vary per request, the generator wins because it only ever touches the reachable region.
  • Which classic graph algorithms do not transfer cleanly to an implicit state space?
    Anything that begins by iterating over all nodes. Finding every component, computing a global degree distribution, or sorting all nodes assumes an enumerable node set, which an implicit graph does not give you cheaply. What does transfer is reachability from a known start: search, shortest paths, and cycle detection along the explored region.
  • Why can a scrambled sliding-tile board be provably unsolvable?
    Because legal moves preserve an invariant combining tile permutation parity with the blank's position. That invariant splits the arrangements into two components with no edge between them, so a scramble in the wrong component can never reach the goal. It is worth naming because it shows the state graph is disconnected, which no amount of searching will fix.

saying these in an interview costs you the question

  • Says you must construct the graph before any search
  • Estimates cost from the whole state space, not the explored region
  • Thinks a node is a single tile rather than the whole board
  • Assumes every arrangement is reachable from every other
  • Claims on-demand generation is always better than precomputing

context