FastPrepFind the Safest Path in a Grid
Problems/Find the Safest Path in a Grid

Find the Safest Path in a Grid

Google logoGoogle● MediumINTERNNEW GRADPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: This was a PhD intern interview. A commenter said the second interview problem was a well-known one, and the original author confirmed it. The exact wording is not available. So we use the usual output, the maximum safeness factor. Our version is about 95% the same as the confirmed 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. 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>>) → 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 valid path has safeness factor 0.

Example 2

grid = [[0,0,1],[0,0,0],[0,0,0]]return = 2

A 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 = 2

There 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 either 0 or 1.
  • 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

Interview reports that mention this question

  • Google SWE and PhD Intern Coding: 14 Experiences (Part 3 of 3)

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int maximumSafenessFactor(List<List<Integer>> grid) {
        // Write your code here.
    }
}
Checking account…