Knight shortest path on a chessboard
Filed under Microsoft · Also reported at MakeMyTrip
Free read 3 of 3Confirm board size and whether coordinates are 0-indexed or 1-indexed.
The question
Given a standard 8x8 chessboard, find the minimum number of steps a knight needs to reach a destination coordinate from a starting coordinate.
Constraints
- Board dimensions are fixed at 8x8 (or generalized to N x N).
- Start and destination coordinates are valid grid locations.
Examples
- Input
- start = (0, 0), dest = (7, 7)
- Output
- 6
- Why
- A knight takes a minimum of 6 optimal moves to travel from top-left to bottom-right corner on an 8x8 board.
Approach
Confirm board size and whether coordinates are 0-indexed or 1-indexed.
Exhaustively try all paths using backtracking up to a depth limit, which is inefficient and misses optimal short paths.
Use Breadth-First Search (BFS) since all edge weights (knight moves) are equal (cost of 1). Track visited squares using a boolean matrix or set to avoid redundant processing.
Start and destination being identical; destination unreachable due to invalid bounds.
Tips
- What they are testing: Test when start equals destination (should return 0) and positions requiring intricate multi-hop detours.
- Common mistake: Using DFS instead of BFS; DFS does not guarantee finding the *minimum* shortest path in an unweighted grid graph.
Follow-ups
- Discuss various corner cases.
Related Microsoft questions
Collected from public candidate posts and coding forums. Updated Nov 2026.