Find the Safest Path in a Grid
Updated
Problem statement
You are given an n x n binary matrix grid. A cell containing 1 contains a thief, and a cell containing 0 is empty. You start at (0, 0) and want to reach (n - 1, n - 1).
In one move, you may travel up, down, left, or right to a cell inside the grid. You may enter a cell containing a thief.
The safeness factor of a path is the minimum Manhattan distance between any cell on that path and any thief in the grid. Return the maximum safeness factor among all paths from the start to the destination.
The Manhattan distance between cells (r1, c1) and (r2, c2) is |r1 - r2| + |c1 - c2|.
Function
maximumSafenessFactor(grid: List<List<Integer>>) → intExamples
Example 1
grid = [[1,0,0],[0,0,0],[0,0,1]]return = 0The start and destination both contain thieves, so every valid path has safeness factor 0.
Example 2
grid = [[0,0,1],[0,0,0],[0,0,0]]return = 2A path along the left and bottom edges always 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 = 2There is a route whose closest approach to either thief is distance 2; every start-to-destination route has a cell no farther than 2 from a thief.
Constraints
1 <= n == grid.length <= 400.grid[i].length == n.grid[i][j]is either0or1.- The grid contains at least one thief.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Linked Merchants Within Two HopsStripe · PHONE SCREENStripe interview questions
- Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Course Schedule IIAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Shortest Travel Time in a Grid with Traffic LightsWaymo · PHONE SCREENWaymo interview questions
- Island Count and Maximum AreaOracle · ONSITE INTERVIEWOracle interview questions
- Minimum Jumps to Reach HomeGoodScore · PHONE SCREEN
- Shortest Increasing Path to a TargetUber · ONSITE INTERVIEWUber interview questions
- Count Islands in a Binary GridApple · PHONE SCREENApple interview questions
- Pacific Atlantic Water FlowAnduril · PHONE SCREENAnduril interview questions
- Detonate Bombs with Chain ReactionsAirbnb · PHONE SCREENAirbnb interview questions