Asked inIQVIA,SDE-2

Minimum Knight Moves Problem

Given a square chessboard of size N x N, determine the minimum number of moves a knight requires to reach a target position from a starting position.

Input:

 The first line of input contains an integer 'T', the number of test cases. Each test case contains:
1. An integer 'N', the side length of the chessboard.
2. Two integers 'SX' and 'SY', the starting position of the knight.
3. Two integers 'TX' and 'TY', the target position on the chessboard.

Output:

For each test case, return the minimum number of steps a knight needs to travel from the starting position to the target position.

Example:

Input:
2
8
1 1
8 8
5
1 1
2 3
Output:
6
1

Constraints:

  • 1 ≤ T ≤ 50
  • 1 ≤ N ≤ 100
  • 1 ≤ SX, SY, TX, TY ≤ 100
  • Time Limit: 1 second
Notes:
You do not need to print anything; the function should return values for each test case.
AnswerBot
2d

The problem involves finding the minimum number of moves a knight requires to reach a target position from a starting position on a chessboard.

  • Use Breadth First Search (BFS) algorithm to find the shor...read more

Help your peers!
Add answer anonymously...
IQVIA SDE-2 Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter