Knight Minimum Moves
Updated
Problem statement
A knight starts at (startRow,startCol) on a square board with n rows and n columns. Coordinates are zero-based.
Each move changes one coordinate by 2 and the other by 1, independently choosing either sign. Every landing square must remain inside the board.
Return the minimum number of moves to (endRow,endCol), or -1 if the target cannot be reached. The board has no obstacles or other pieces.
Function
minMoves(n: int, startRow: int, startCol: int, endRow: int, endCol: int) → intExamples
Example 1
n = 9startRow = 4startCol = 4endRow = 4endCol = 8return = 2Use legal two-by-one knight jumps that stay inside the square board. No obstacle or bishop is present.
Example 2
n = 4startRow = 0startCol = 0endRow = 1endCol = 2return = 1Use legal two-by-one knight jumps that stay inside the square board. No obstacle or bishop is present.
Example 3
n = 4startRow = 1startCol = 1endRow = 1endCol = 1return = 0Use legal two-by-one knight jumps that stay inside the square board. No obstacle or bishop is present.
Constraints
4 <= n <= 150- All start and end coordinates satisfy
0 <= coordinate < n.
More IMC Trading Online Assessment questions
- Earliest Meeting Start on a 24-Hour ClockOA · Seen Nov 2026
- Grid Paths with Override PassesOA · Seen Oct 2026
- Weighted LFU CacheOA · Seen Sep 2026
- Rock JumpingOA · Seen Aug 2026
- Stack Batch RemovalOA · Seen Aug 2026
- Chain of CommandOA · Seen Aug 2026
- Choose ContainersOA · Seen Aug 2026
- Sub-matrix SumsOA · Seen Jul 2026
- Sub-matrix SumsOA · Seen Jul 2026
- Can Every Package Fit in a BoxOA · Seen Jul 2026
Similar questions at other companies
- Base Unit ConversionsAdobe · OAAdobe interview questions
- Delivery Management SystemAkuna Capital · OAAkuna Capital interview questions
- Minimize CommuteDatabricks · OA · PHONE SCREENDatabricks interview questions
- Tree Pythagorean TriplesIBM · OAIBM interview questions
- Shortest Atom Action PathOpenAI · OAOpenAI interview questions
- Distances to CyclesBelvedere Trading · OA
- Shortest Path from an Encoded GraphIntuit · OAIntuit interview questions
- Minimize Malware Spread by Removing a NodeVisa · OAVisa interview questions
- Longest Distance in a Frequency-Limited NetworkBloomberg LP · OABloomberg LP interview questions
- Minimum Cleaning Robot RunsHSBC · OAHSBC interview questions