Quant Memo
Core

The Spider and the Fly: Unfolding a Box

A classic puzzle about the shortest path across the surfaces of a box, solved by unfolding the box flat into a plane so a curved surface-path becomes an ordinary straight line.

A spider sits on the inside wall of a rectangular room, a fly sits on the opposite wall, and both are stuck to the room's surfaces — the spider can only crawl along walls, floor, and ceiling, never fly through the air. What's the shortest crawling path from spider to fly? The room is a box, and paths along a box's surface bend at every edge you cross, which makes "shortest path" look like it needs calculus on a folded surface. It doesn't — it needs one clever trick: unfold the box flat.

The unfolding trick

A path that crawls across several faces of a box, when you "unglue" those faces at their shared edges and lay them flat in a single plane (keeping each face's shape rigid, just rotating it around the hinge edge), becomes an ordinary path in a flat 2-D picture. If the original crawling path was straight within each face and only bent where it crossed an edge, then in the unfolded picture — where those edges are no longer forced bends — the shortest such path is simply the straight line between the unfolded positions of the two endpoints. Since the straight line is provably the shortest path between two points in a plane, and unfolding preserves all distances within each face exactly, the shortest crawling path is found by unfolding the right sequence of faces and drawing a ruler-straight line.

The catch: there are several different sequences of faces the spider could cross (over the top, or around a side, or across the far wall), each giving a different unfolding and a different straight-line distance. You compute the straight-line distance for each plausible unfolding and take the minimum.

Worked example: comparing two unfoldings on a cube

Take a cube with 10-foot edges. The spider sits on one face, 2 feet from one edge and 2 feet from an adjacent edge — near a corner. The fly sits on the opposite face of the cube, at the mirror-image position near the diagonally farthest corner. Unfolding A: cross straight across one adjoining face to reach the fly's face. Unfolding the two faces into a flat rectangle, the spider's point and the fly's point end up 1818 feet apart horizontally and 66 feet apart vertically (working through the 2-foot offsets and the two 10-foot edges), giving straight-line distance 182+62=324+36=36018.97\sqrt{18^2+6^2}=\sqrt{324+36}=\sqrt{360}\approx18.97 feet. Unfolding B: cross two adjoining faces instead of one, going around a different pair of edges. That unfolding lays the same two points out 2222 feet apart horizontally and 22 feet apart vertically, giving 222+22=484+4=48822.09\sqrt{22^2+2^2}=\sqrt{484+4}=\sqrt{488}\approx22.09 feet — clearly longer. Unfolding A wins, roughly 19 feet versus roughly 22 feet, and the only way to know that in advance is to lay out and measure both candidates, since neither one is obviously shorter just by looking at the folded box.

end wall ceiling (unfolded) side wall (unfolded) far wall spider fly
Once the box's faces are unfolded flat into one plane, the shortest surface-crawling path is simply a straight line — the puzzle is finding which sequence of faces to unfold.

What this means in practice

Unfolding is a specific instance of a broader interview habit: when a constraint (crawl only on surfaces, move only along a grid, respect a barrier) makes a shortest-path problem awkward in its native geometry, look for a transformation that removes the constraint and turns it into a problem you already know how to solve (here, straight-line distance). The same reflex — re-coordinate the problem until it's simple — shows up in reflection tricks for "shortest path bouncing off a wall" puzzles and in re-parameterizing constrained optimization problems to remove a boundary.

The shortest path across the surfaces of a box is found by unfolding the sequence of faces it crosses into a single flat plane, then drawing a straight line between the endpoints — check every plausible unfolding sequence, since the shortest one is often not the visually "obvious" route.

Related concepts

Practice in interviews

Further reading

  • Dudeney, Amusements in Mathematics (1917)
ShareTwitterLinkedIn