Minimum Steps for a Knight to Reach Target
Given a square chessboard of size N x N
, you need to determine the minimum number of steps a Knight takes to reach a target position from its starting position.
Input:
The first line of input contains an integer ‘T’ representing the number of test cases.
Each test case consists of three lines:
- The first line contains two integers representing the starting position of the Knight.
- The second line contains two integers denoting the target position.
- The third line contains an integer ‘N’ denoting the size of the chessboard.
Output:
For each test case, output a single integer indicating the minimum number of steps the Knight requires to reach the target position.
Example:
knightPosition: {3,4}
targetPosition: {2,1}
The knight can move from (3,4) to positions (1,3), (2,2), and (4,2). Select (4,2), making 'stepCount' 1. From (4,2), the knight can jump to (2,1), reaching the target in 2 steps. Therefore, the answer is 2.
Constraints:
1 ≤ T ≤ 10
1 ≤ N ≤ 1000
1 ≤ KNIGHTPOSITION(X, Y), TARGETPOSITION(X, Y) ≤ N
- Time limit: 1 second
Note:
You are not required to print the output manually. Implement the function to return the result.

AnswerBot
4mo
Find the minimum number of steps a Knight takes to reach a target position on a chessboard.
Use BFS algorithm to find the shortest path from knight's starting position to target position.
Consider all p...read more
Help your peers!
Add answer anonymously...
Nagarro Software Developer interview questions & answers
A Software Developer was asked 8mo agoQ. Explain Kafka and how you would implement it.
A Software Developer was asked 9mo agoQ. Is the directory a file?
A Software Developer was asked 9mo agoQ. What is memory segmentation?
Popular interview questions of Software Developer
A Software Developer was asked 8mo agoQ1. Explain Kafka and how you would implement it.
A Software Developer was asked 9mo agoQ2. Is the directory a file?
A Software Developer was asked 9mo agoQ3. What is memory segmentation?
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

