Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Knight Minimum Moves

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Knight Minimum Moves. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

In a Citadel trading-grid simulation, a knight moves across an n x n board using standard chess moves: two squares in one direction and one square perpendicular to it. Given the board size, starting square, and target square, return the minimum number of moves needed to reach the target.

Every square is traversable. Return 0 when the starting and target squares are identical.

Formal Specification

Implement min_knight_moves(n, start, target).

  • n is an integer board size.
  • start and target are two-element lists [row, column], using zero-based coordinates.
  • Return an integer representing the minimum number of legal knight moves.

Use breadth-first search because every move has equal cost, so the first time a square is visited is through a shortest path.

Constraints

  • 1 <= n <= 500
  • start and target contain exactly two coordinates
  • All coordinates are zero-based and lie within the board
  • Every square is traversable

Function Signature

def min_knight_moves(n, start, target):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output