This practice workspace is built for desktop. Open it on a larger screen to start solving.
Minimum Knight Moves
In an infinite chess board with coordinates from -infinity to +infinity, you have a knight at square (0, 0).
A knight has 8 possible moves it can make, as illustrated by the L-shaped jumps (±1, ±2) and (±2, ±1).
Return the minimum number of steps needed to move the knight to the square (x, y). It is guaranteed the answer exists.
Hint. By symmetry the problem reduces to the first quadrant (x = abs(x), y = abs(y)). A standard BFS with a small slack (e.g. allow one step into negative territory) suffices for the constraint range. Memoization or bidirectional BFS is optional.
Examples
Constraints
- -300 <= x, y <= 300
- (x, y) is reachable from (0, 0).
Preview Mode
This is a read-only preview of DSAMind's AI coaching. Sign up to get custom feedback on your own solution.
Your approach
- Pattern:Breadth-First Search
- Time:O(N)
- Space:O(1)
Complete approach — pattern and complexity both named.
Coach
What you got right
You correctly matched the sorted input requirement and implemented a linear-time scan using two pointers.
Where it diverged
No divergence detected. The code correctly aligns with the stated approach.
Next attempt: focus on
Try solving related sliding window problems to build familiarity with two-pointer variants.
Signals
No signals fired — clean run, you stayed in flow.