FastPrepKnight Minimum Moves with a Capturable Bishop
Problems/Knight Minimum Moves with a Capturable Bishop

Knight Minimum Moves with a Capturable Bishop

IMC Trading logoIMC Trading● MediumINTERNNEW GRADOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an IMC Trading online assessment. The full task lets the bishop be captured and lets the destination be under threat. It gives the board limits and examples. We keep the existing method name. If the knight and bishop start on the same square, we treat the bishop as already captured. The report does not say what happens in that case. Our version is about 99% the same as the reported exercise.

You are given an n-by-n chessboard with rows and columns numbered from 0 to n - 1. A knight starts at (startRow, startCol), must reach (endRow, endCol), and shares the board with a bishop at (bishopRow, bishopCol).

A knight moves by changing one coordinate by 2 and the other by 1, with either sign. Every landing square must remain inside the board. While the bishop is present, the knight may not land on a square on either diagonal through the bishop, with two exceptions: it may land on the bishop's own square to capture it, and it may land on the target even if that target is threatened.

After the bishop is captured, its diagonal restrictions disappear for the rest of the route. The bishop never moves. The initial square is permitted even if threatened because the restriction applies to subsequent landings. For this exercise, if the knight and bishop initially share a square, treat the bishop as already captured.

Return the minimum number of knight moves needed to reach the target, or -1 if no route exists. If the start is already the target, return 0.

Function

minKnightMoves(n: int, startRow: int, startCol: int, endRow: int, endCol: int, bishopRow: int, bishopCol: int) → int

Examples

Example 1

n = 8startRow = 4startCol = 2endRow = 2endCol = 6bishopRow = 2bishopCol = 3return = 4

One shortest route is (4,2) → (2,3) → (0,2) → (1,4) → (2,6). The first move captures the bishop, so (1,4) becomes legal. A four-move route avoiding capture also exists.

Example 2

n = 6startRow = 0startCol = 0endRow = 0endCol = 2bishopRow = 0bishopCol = 1return = 2

The route (0,0) → (2,1) → (0,2) uses two legal knight moves. The intermediate square is not threatened.

Example 3

n = 6startRow = 5startCol = 1endRow = 0endCol = 5bishopRow = 2bishopCol = 3return = 3

The route (5,1) → (4,3) → (2,4) → (0,5) reaches the target in three moves without landing on a threatened intermediate square.

Example 4

n = 9startRow = 4startCol = 4endRow = 4endCol = 8bishopRow = 0bishopCol = 1return = 2
Example 4 illustration

The route (4,4) → (3,6) → (4,8) uses two legal knight moves.

Example 5

n = 5startRow = 0startCol = 0endRow = 4endCol = 3bishopRow = 2bishopCol = 2return = 3

The initial square is threatened, but it is permitted. The route (0,0) → (1,2) → (2,4) → (4,3) reaches the target in three moves.

Constraints

  • 4 <= n <= 150
  • 0 <= startRow, startCol, endRow, endCol, bishopRow, bishopCol < n
  • The bishop stays on its square until captured; after capture it attacks no squares.
  • A threatened target is a legal final landing, even before the bishop is captured.
  • The initial square may be threatened. For this exercise, an initial knight-bishop overlap means the bishop is already captured.

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

  • Drone Delivery with Battery Swap StationsCisco · OACisco interview questions
  • Shortest Travel Time in a Grid with Traffic LightsWaymo · PHONE SCREENWaymo interview questions
  • Fire Station Response BillLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • Shortest Increasing Path to a TargetUber · ONSITE INTERVIEWUber interview questions
  • Minimum Distance to Return a BookGoogle · ONSITE INTERVIEWGoogle interview questions
  • Find and Reconstruct a Binary-Matrix PathMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
  • Delivery Management SystemAkuna Capital · OAAkuna Capital interview questions
  • Minimize CommuteDatabricks · OA · PHONE SCREENDatabricks interview questions
  • Minimum MovesHarness · OA
  • Shortest Atom Action PathOpenAI · OAOpenAI interview questions

Interview reports that mention this question

  • IMC Trading OA: What It Tests (2026)
  • IMC Trading SWE OA and Video: 18 Experiences (Part 3 of 3)
  • IMC Trading SWE OA Questions: 22 Experiences (Part 2 of 3)
  • IMC SWE New Grad OA and Interviews: 58 Experiences (Part 1 of 3)
All IMC Trading interview questionsSee IMC Trading hiring insights
Saved
class Solution {
    public int minKnightMoves(int n, int startRow, int startCol, int endRow, int endCol, int bishopRow, int bishopCol) {
        // Write your solution here.
    }
}
Checking account…