Reported September 2021
IMCbreadth first search

Knight Minimum Moves with a Fixed Bishop

Reported by candidates from IMC's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live IMC OA. Under 2s to a working solution.
Founder's read

The IMC OA reported in September 2021 hinges on a queue. It's a shortest-path problem on a grid, so BFS is the whole game. A knight crosses an n-by-n board while a fixed bishop blocks its square and both diagonals through it. If you've seen knight BFS, you're 80% there. The twist is the blocked-square check, and the early -1 when the start or target is unsafe. Boards go up to 150 by 150, so a plain BFS is fine. If you blank on the live OA, StealthCoder sits invisibly on your screen as a safety net and hands you the structure.

The problem

You are given an n-by-n chessboard whose rows and columns are numbered from 0 to n - 1. A knight starts at (startRow, startCol) and must reach (endRow, endCol).
A bishop remains fixed at (bishopRow, bishopCol). The knight may not occupy the bishop's square or any square the bishop attacks. Because the board contains no other pieces, the bishop attacks every in-bounds square on either diagonal through its position.
On each move, the knight changes its row by 2 and its column by 1, or its row by 1 and its column by 2, with either sign. Return the minimum number of legal knight moves needed to reach the target. Return -1 if the start or target is unsafe, or if the target cannot be reached.

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

Examples
Example 1
n = 9
startRow = 4
startCol = 4
endRow = 4
endCol = 8
bishopRow = 0
bishopCol = 1
return = 2
The target is reachable in two legal knight moves, and neither visited square lies on a bishop diagonal.
Example 2
n = 5
startRow = 0
startCol = 0
endRow = 4
endCol = 3
bishopRow = 2
bishopCol = 2
return = -1
The starting square lies on the bishop's diagonal, so no legal route exists.

Constraints
4 <= n <= 150
0 <= startRow, startCol, endRow, endCol, bishopRow, bishopCol < n
The bishop remains fixed for the entire route.
The knight may not occupy the bishop's square or any square on either bishop diagonal.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Run BFS from the start square with a queue and a visited grid. Each pop expands eight knight moves. Skip any move that's out of bounds, visited, or unsafe. A square is unsafe if it's the bishop's square or if abs(r - bishopRow) equals abs(c - bishopCol), which covers both diagonals without precomputing anything. BFS gives the minimum move count because every move costs one. The common pitfalls: forgetting to check the start and target before searching, which is exactly what Example 2 tests. Also mark squares visited when you enqueue, not when you dequeue, or the queue balloons. And if start equals target and it's safe, return 0. That's the edge case people skip. The grid has at most 22,500 cells, so time isn't a worry. If your mind goes blank mid-assessment, StealthCoder is the hedge that surfaces the BFS skeleton while you focus on the unsafe-square check.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Knight Minimum Moves with a Fixed Bishop cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass IMC's OA.

IMC reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Knight Minimum Moves with a Fixed Bishop FAQ

What's the trick in the IMC knight and bishop problem?+

BFS on the board, with a one-line unsafe test. A square is blocked when its row difference from the bishop equals its column difference. That single check covers the bishop's square and both diagonals. Everything else is a standard knight BFS.

How hard is this OA really?+

Medium at most. If you know knight BFS from the classic grid problems, the only new piece is the diagonal check. The traps are edge cases, not the algorithm. Expect to spend your time on correctness, not on cleverness.

Why BFS and not DFS or DP?+

Every move costs exactly one, and you want the fewest moves. BFS explores in layers, so the first time you reach the target is the shortest path. DFS would need to explore all paths and track the best, which is slower and messier.

What edge cases should I test before submitting?+

Start unsafe, target unsafe, start equal to target, and a target that's safe but walled off by diagonals. Example 2 covers the unsafe start. Also test the smallest board, n = 4, where the bishop's diagonals can cut off large regions.

How do I prepare in 48 hours?+

Write knight BFS from scratch twice, with a queue, a visited array, and a direction list of eight pairs. Then add the unsafe check and the early -1 returns. Test both examples by hand. That's enough for this problem, since the pattern is common.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with IMC.

OA at IMC?
Invisible during screen share
Get it