Minimum Moves for Two Knights to Meet
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Amazon OA from September 2026 hands you two knights on an infinite board and asks how fast they can meet. The trap is the one that kills naive solutions: coordinates go up to 10^9, so BFS is dead on arrival, and the closed-form formula has exceptions that fail quietly. It's a math problem wearing a graph costume. If you blank on the formula mid-assessment, StealthCoder runs invisibly on your desktop and gives you the working solution in real time. Read on for the trick and the exact edge cases.
The problem
Two knights start at coordinates first = [x1, y1] and second = [x2, y2] on an infinite chessboard. They take turns, with the first knight moving first. On a turn, the chosen knight must make one standard knight move: two squares along one axis and one square along the other. Return the minimum total number of moves until the two knights occupy the same coordinate. If they already share a coordinate, return 0. Function minimumKnightMeetingMoves(first: int[], second: int[]) → int Examples Example 1 first = [0,0] second = [1,2] return = 1 The first knight can reach the second knight in one move. Example 2 first = [0,0] second = [1,0] return = 3 The adjacent displacement is the exceptional three-move case. Example 3 first = [7,-4] second = [7,-4] return = 0 The knights already meet. Constraints first.length == second.length == 2 -10^9 <= first[i], second[i] <= 10^9
Reported by candidates. Source: FastPrep
Pattern and pitfall
Alternating turns doesn't change the answer. Two knights closing a gap in total moves is the same as one knight traveling the displacement (dx, dy). Take absolute values, swap so dx >= dy, and you're solving the classic infinite-board knight distance. Special cases first: (0,0) is 0, (1,0) is 3, and (2,2) is 4. Otherwise compute delta = dx - dy. If dy > delta, the answer is delta - 2*floor((delta - dy) / 3). If not, it's delta - 2*floor((delta - dy) / 4). Hmm, that's the sign-flipped version people mix up, so test it against your examples. The pitfall is skipping the exceptions, or running BFS with a bounded grid and timing out. Example 2 is the (1,0) case, so it's a hint the grader checks it. With StealthCoder as your hedge in the live OA, you'd still want to verify these three exceptions yourself.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Minimum Moves for Two Knights to Meet 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 StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon 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.
Minimum Moves for Two Knights to Meet FAQ
What's the trick in the Amazon two knights meeting problem?+
Reduce it to one knight. The total moves for both knights to meet equals the moves one knight needs to cover the displacement between them. Then use the closed-form infinite-board knight distance with absolute values and a swap so dx >= dy, instead of running BFS.
Why can't I just use BFS?+
Coordinates reach 10^9 in magnitude, so the displacement can be huge. A BFS would visit far too many cells. A bounded BFS near the origin only works for small offsets and fails on large inputs. You need a formula, or a formula for the bulk plus BFS for tiny cases.
Which edge cases break a naive formula?+
Identical positions return 0. A displacement of (1,0) needs 3 moves, which Example 2 shows. A displacement of (2,2) needs 4 moves. These small offsets violate the general formula, so hardcode them before applying it. Also handle negative coordinates by taking absolute differences.
Does the turn order matter?+
No. Each move by either knight reduces the displacement exactly like a single knight moving. The first knight moving first only matters for who moves, not for the minimum total count. So you can ignore the alternation and compute one distance.
How do I prepare for this in 48 hours?+
Write the single-knight distance function on paper and test it on offsets like (0,0), (1,0), (1,2), (2,2), and a large one like (10^9, 0). Check parity and overflow in your language. Spend the rest of your time on other math and BFS patterns, since this type of problem repeats.