As reported: DocuSign software engineering intern online assessment on HackerRank (2025), question 3: a grid of empty and blocked cells; find a path that maximises the minimum distance from the path's cells to any blocked cell, and return that maximised minimum distance.
This is LeetCode 2812 — Find the Safest Path in a Grid.
You are given an n × n grid where grid[r][c] = 1 is a blocked cell and 0 is an empty cell. You start at (0, 0) and must reach (n − 1, n − 1), moving up, down, left or right.
The safeness of a path is the minimum, over all cells on the path, of the Manhattan distance from that cell to the nearest blocked cell. Return the maximum safeness over all paths. (The Manhattan distance between (a, b) and (x, y) is |a − x| + |b − y|.)
Example 1
grid = [[1,0,0],[0,0,0],[0,0,1]] -> 0
The start and the end are blocked cells themselves, so every path has safeness 0.
Example 2
grid = [[0,0,1],[0,0,0],[0,0,0]] -> 2
Go down the left column and along the bottom row: the closest cell to the blocked (0, 2) is (0, 0) or (2, 2), at distance 2.
Example 3
grid = [[0,0,0,1],[0,0,0,0],[0,0,0,0],[1,0,0,0]] -> 2
Clarifications
- A path may pass through a blocked cell, but that cell has distance 0, so its safeness is 0.
- There is at least one blocked cell.
Constraints: 1 ≤ n ≤ 400; grid[i][j] is 0 or 1.
Code runner format: one line holding grid (LeetCode's test-case format). Implement maximumSafenessFactor(grid); the harness prints the answer.