Find Shortest Knight Path
Company: Waymo
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates a candidate's ability to model grid-based movement as a graph and apply shortest-path traversal techniques, testing algorithmic problem-solving, state representation, and complexity analysis in the Coding & Algorithms domain.
Part 1: Find the Shortest Knight Path on a Bounded N x N Chessboard
Constraints
- 1 <= n <= 500
- start and target are tuples (r, c) with 0 <= r, c < n
- Use 0-based board coordinates
Examples
Input: (8, (0, 0), (1, 2))
Expected Output: 1
Explanation: The target is exactly one legal knight move away.
Input: (4, (0, 0), (3, 3))
Expected Output: 2
Explanation: One shortest path is (0, 0) -> (1, 2) -> (3, 3).
Input: (1, (0, 0), (0, 0))
Expected Output: 0
Explanation: Start and target are the same cell.
Input: (2, (0, 0), (1, 1))
Expected Output: -1
Explanation: On a 2 x 2 board, a knight cannot make any legal move from (0, 0).
Input: (8, (0, 0), (7, 7))
Expected Output: 6
Explanation: A shortest path exists in 6 moves on a standard 8 x 8 board.
Hints
- Model each cell as a graph node and each legal knight move as an edge.
- Because every move has the same cost, breadth-first search gives the shortest path.
Part 2: Find the Shortest Knight Path When Some Cells Are Blocked
Constraints
- 1 <= n <= 500
- start and target are tuples (r, c) with 0 <= r, c < n
- blocked contains 0 or more board cells
- Use 0-based board coordinates
Examples
Input: (6, (0, 0), (5, 5), [(1, 2)])
Expected Output: 4
Explanation: The blocked cell removes one immediate option, but a shortest path still exists in 4 moves, such as (0, 0) -> (2, 1) -> (4, 2) -> (3, 4) -> (5, 5).
Input: (5, (0, 0), (4, 4), [(1, 2), (2, 1)])
Expected Output: -1
Explanation: From (0, 0), the only legal knight moves are (1, 2) and (2, 1), and both are blocked.
Input: (5, (2, 2), (2, 2), [])
Expected Output: 0
Explanation: Start and target are the same cell, so no moves are needed.
Input: (5, (0, 0), (4, 4), [(4, 4)])
Expected Output: -1
Explanation: The target cell is blocked, so it cannot be visited.
Input: (5, (0, 0), (4, 4), [(0, 0)])
Expected Output: -1
Explanation: The start cell is blocked, so the knight cannot begin.
Hints
- This is still a shortest-path problem on an unweighted graph, so BFS is a strong fit.
- Treat blocked cells as removed nodes, and check the start and target before running the search.
Part 3: Find the Shortest Knight Path on an Infinite Chessboard
Constraints
- -10^9 <= sx, sy, tx, ty <= 10^9
- Coordinates are integers
- The board is infinite, so every target is reachable
Examples
Input: ((0, 0), (0, 0))
Expected Output: 0
Explanation: No movement is needed.
Input: ((0, 0), (1, 2))
Expected Output: 1
Explanation: This is one direct knight move.
Input: ((0, 0), (1, 0))
Expected Output: 3
Explanation: A knight cannot move one square straight in fewer than 3 moves.
Input: ((0, 0), (2, 2))
Expected Output: 4
Explanation: This is a known special case for knight distance on an infinite board.
Input: ((0, 0), (7, 7))
Expected Output: 6
Explanation: A larger example that shows why a direct formula is useful.
Hints
- Use symmetry: only the absolute difference between the coordinates matters, and you can reorder so x >= y.
- There are a couple of small special cases, and then parity helps adjust the final answer.