Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Knight Minimum Moves
00:00
5 left

Knight Minimum Moves

HardPython

Problem

Given an N by N chess board with a knight and a king, find the minimum number of moves for the knight to get to the king.

Implement min_knight_moves(n, knight, king), where coordinates are zero-indexed [row, column] arrays. Return the minimum move count, or -1 when the king is unreachable.

Constraints

  • 1 <= n <= 1000
  • knight and king are arrays of two integers
  • 0 <= knight[0], knight[1], king[0], king[1] < n
  • The knight and king may initially occupy the same square
  • Return -1 if the target is unreachable

Function Signature

def min_knight_moves(n, knight, king):
Interviewer

Your question is Knight Minimum Moves. Start with the requirements in the Question tab.

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.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.