Minimum Knight Moves

MediumGraphsbfs

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

A knight starts at [0, 0] on an infinite chessboard. In one move it jumps two squares in one axis direction and one square in the perpendicular direction (8 possible moves). Return the minimum number of moves needed to reach the square [x, y]. Every square is reachable.

Examples

Input: x = 2, y = 1
Output: 1
[0,0] -> [2,1].
Input: x = 5, y = 5
Output: 4
[0,0] -> [2,1] -> [4,2] -> [3,4] -> [5,5].

Constraints

  • `-300 <= x, y <= 300`
  • `0 <= |x| + |y| <= 300`

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More bfs problems