Magicsheet logo

The Knight’s Tour

Medium
37.5%
Updated 8/1/2025

Asked by 1 Company

The Knight’s Tour

What is this problem about?

The Knight's Tour is a classic mathematical challenge and a famous chess-based puzzle. A knight is placed on an empty board of 'n' by 'm' dimensions. The objective is to move the knight according to the standard rules of chess such that it visits every single square on the board exactly once. A knight moves in an "L" shape: two squares in one cardinal direction and then one square perpendicularly. This problem asks you to find a sequence of moves that completes this tour starting from a given position. It is a specific instance of the Hamiltonian path problem on a graph where the board squares are vertices and legal knight moves are edges.

Why is this asked in interviews?

This the Knight’s Tour interview question is a rigorous test of a candidate's mastery of backtracking and recursion. It is often asked at companies like Microsoft to evaluate how well a developer can manage state and explore a large search space. It also provides an opportunity for candidates to discuss heuristics and optimization. Because a naive search can take an astronomical amount of time, interviewers look for candidates who understand how to prune search trees and potentially apply optimizations like Warnsdorff's rule (choosing the move that leads to the cell with the fewest onward moves).

Algorithmic pattern used

This problem is a quintessential example of the Array, Matrix, Backtracking interview pattern. The algorithm typically involves a recursive function that attempts to make a move, marks the current square as visited, and then calls itself to find the next move. If the knight reaches a point where no legal moves to unvisited squares are available, the algorithm "backtracks"—unmarking the current square and returning to the previous step to try a different path. The state is maintained by a 2D matrix representing the board, where each cell stores the order in which it was visited.

Example explanation

On a small 5x5 board starting at (0,0):

  1. The knight marks (0,0) as step 1.
  2. Legal moves from (0,0) include (1,2) and (2,1).
  3. The algorithm tries (1,2) and marks it as step 2.
  4. From (1,2), it might go to (3,3) and mark it as step 3.
  5. This continues until step 25 is reached, at which point the tour is complete.
  6. If the knight reaches (4,4) at step 10 but has no unvisited neighbors, it will backtrack to step 9 and try a different move. On very small boards like 3x3, a full tour is impossible. On a standard 8x8 board, there are trillions of solutions, yet finding one requires a very efficient search strategy.

Common mistakes candidates make

The most common mistake in "The Knight’s Tour coding problem" is failing to correctly implement the base case for the recursion. Some might stop too early or fail to recognize when a dead end has been reached. Another issue is forgetting to "un-visit" a square during the backtracking phase, which prevents the algorithm from exploring alternative paths. Furthermore, without a heuristic, the search can be extremely slow; candidates who don't at least mention the possibility of ordering moves to prioritize "constrained" squares might be seen as less experienced in algorithm optimization.

Interview preparation tip

To prepare for backtracking problems, focus on understanding the recursive stack. Practice drawing out the decision tree for small board sizes to visualize how the algorithm explores and retreats. Learning about heuristics like Warnsdorff's rule can set you apart from other candidates, as it demonstrates a proactive approach to performance. Finally, ensure you are comfortable working with 2D arrays and managing boundary conditions for complex movements like the knight's "L" jump.

Similar Questions