Minimum Knight Moves to a Square on an Infinite Chessboard

Last updated: November 29, 2026

Quick Overview

A knight starts at (0, 0) on an infinite chessboard. Given a target (x, y), return the minimum number of knight moves needed to reach it.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Medium

0

0

0 solved


A knight starts at (0, 0) on an infinite chessboard. Given a target (x, y), return the minimum number of knight moves needed to reach it.

A phone screen question where the graph is infinite, so the interviewer wants to see how you bound the search. Strong candidates use symmetry to fold the target into one quadrant, prune the search region, and know that BFS on unweighted moves gives the optimum.

What the Interviewer Expects
  • Model squares as nodes and the 8 knight jumps as edges
  • Use BFS for the shortest path
  • Exploit symmetry by taking absolute values
  • Bound the search to a small margin around the target quadrant
  • Discuss bidirectional BFS or a closed-form for very large targets
Key Topics to Cover
BFS
implicit infinite graph
symmetry
search pruning
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Make it work for |x|, |y| up to 10^9.
  • Use bidirectional BFS and compare node counts.
  • What if some squares are blocked?
  • Return one optimal sequence of moves.
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: integers x, y (say ∣x∣+∣y∣≤300|x| + |y| \le 300).
  • Output: minimum moves.
  • Board is unbounded, so the search must be bounded by reasoning, not by the board....
High-Level Design

BFS on an infinite grid would expand forever in the wrong direction, so I bound it. By symmetry, the answer for (x, y) equals the answer for (|x|, |y|), so I solve in the first quadrant. An optima...


Submit Your Answer
Markdown supported