Reported September 2026
Amazonbreadth first search

Find the Safest Path in a Grid

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

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

Amazon reported this one in September 2026, and the detail that trips people is right in the statement: a path may walk straight through a thief cell, it just scores 0 there. So you're maximizing the minimum distance to any thief along a route on an n x n grid, with n up to 400. It's a multi-source BFS plus a second search over the answer. If your OA invite lands this week, learn the two-phase shape now. StealthCoder sits invisibly on your screen as a safety net if the second phase goes blank mid-assessment.

The problem

You are given an n x n binary matrix grid. A cell containing 1 contains a thief, and a cell containing 0 is empty.
Start at (0, 0) and move to (n - 1, n - 1). Each move goes one cell up, down, left, or right, and a path may pass through a thief cell.
The safeness factor of a path is the minimum Manhattan distance from any cell on that path to any thief in the grid. Return the maximum safeness factor among all paths from the start to the destination.
The Manhattan distance between (r, c) and (x, y) is |r - x| + |c - y|.

Function
maximumSafenessFactor(grid: int[][]) → int

Examples
Example 1
grid = [[1,0,0],[0,0,0],[0,0,1]]
return = 0
The start and destination both contain thieves, so every path has safeness factor 0.
Example 2
grid = [[0,0,1],[0,0,0],[0,0,0]]
return = 2
A path through the left and bottom area stays at least Manhattan distance 2 from the thief at (0, 2), and no path can do better.
Example 3
grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]]
return = 2
A route through the central corridor remains at least distance 2 from both corner thieves.

Constraints
1 <= grid.length == n <= 400
grid[i].length == n
grid[i][j] is 0 or 1.
The grid contains at least one thief.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Phase one: run a multi-source BFS from every thief cell at once to fill a dist grid with the Manhattan distance to the nearest thief. Phase two: find the path from (0,0) to (n-1,n-1) that maximizes the minimum dist along it. Two clean ways. Binary search a threshold t and BFS only through cells with dist >= t, or use a max-heap Dijkstra variant where you pop the cell with the best bottleneck so far. Both fit n = 400. Common pitfalls: running a separate BFS per thief (far too slow), forgetting that if the start or end cell has dist 0 the answer is 0, and treating thief cells as walls when they're just low-score cells. Because BFS on a grid gives Manhattan distance with four-direction moves, you don't need a formula per pair. If you freeze on the bottleneck step, StealthCoder is the hedge during the live OA.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Find the Safest Path in a Grid 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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as find the safest path in a grid. If you have time before the OA, drill that.

⏵ The honest play

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

Amazon reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Find the Safest Path in a Grid FAQ

What's the trick in Find the Safest Path in a Grid?+

Split it in two. Multi-source BFS from all thieves gives each cell its distance to the nearest thief. Then find the path maximizing the minimum of those values, using binary search with BFS or a max-heap Dijkstra. Don't compute distances per thief.

How hard is this Amazon OA question really?+

Medium-hard. Each piece is standard, but you have to combine two ideas and spot the bottleneck-path framing. If you know multi-source BFS and either binary search on the answer or Dijkstra with a max-heap, it's very doable.

Binary search or Dijkstra, which should I pick?+

Either passes at n = 400. Binary search on t with a BFS check is easier to reason about. Max-heap Dijkstra is a single pass and feels cleaner once you've seen it. Pick the one you can write without bugs under pressure.

What edge cases should I test?+

If grid[0][0] or grid[n-1][n-1] is a thief, return 0 immediately. Also test n = 1 with a thief, which returns 0, and grids with a single thief where the best path hugs the far corner. Example 1 covers the start-on-thief case.

How do I prepare in 48 hours?+

Write multi-source BFS on a grid from memory, then write one bottleneck-path solution both ways. Practice the same two-phase pattern on a similar grid problem. Focus on getting the BFS queue initialized with every thief before the first pop.

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

OA at Amazon?
Invisible during screen share
Get it