Find the grid path that minimizes the maximum detection risk

Quick Overview

Find a route through a grid with obstacles, from the top-left cell to the bottom-right cell, that keeps the highest detection risk on the route as low as possible. Tests modeling a grid as a graph and adapting shortest-path search to a bottleneck cost.

Find the grid path that minimizes the maximum detection risk

Company: Okta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given an `m x n` grid where each cell holds a detection risk value. You start at the top-left cell `(0, 0)` and need to reach the bottom-right cell `(m-1, n-1)`, moving one step at a time up, down, left or right. Cells marked `-1` are obstacles and cannot be entered. The risk of a path is the largest detection risk among the cells on it, including the start and end cells. Find the path that minimizes this maximum risk. Return the minimum possible maximum detection risk. If there is no valid path from start to end, return `-1`. ### Function Signature ```python def sneak_path(grid: list[list[int]]) -> int: ``` ### Rules - `grid` is a rectangular list of rows. A value of `-1` marks an obstacle; every other value is that cell's risk. - The start and end cells count toward the path's maximum. - If the grid is a single cell, the path is that cell alone and the answer is its value. - Return `-1` when the end cell cannot be reached from the start. ### Constraints - `1 <= m, n <= 100` - `-1 <= grid[i][j] <= 1000` - `grid[0][0] != -1` and `grid[m-1][n-1] != -1` ### Examples ```text Input: grid = [[0, 2, 5], [3, -1, 1], [1, 4, 2]] Output: 4 ``` Going right along the top row and then down the right column visits risks 0, 2, 5, 1, 2, so its maximum is 5. Going down the left column and then right along the bottom row visits 0, 3, 1, 4, 2, with maximum 4. Every route into the end cell passes through either the 5 at `(0, 2)` or the 4 at `(2, 1)`, so 4 is the best possible. ```text Input: grid = [[0, 1], [-1, 3]] Output: 3 ``` The obstacle at `(1, 0)` leaves one route, `(0, 0) -> (0, 1) -> (1, 1)`, with risks 0, 1, 3. Its maximum is 3. ```text Input: grid = [[0, -1], [-1, 0]] Output: -1 ``` Both neighbors of the start cell are obstacles, so the end cell cannot be reached.

Overview: Find a route through a grid with obstacles, from the top-left cell to the bottom-right cell, that keeps the highest detection risk on the route as low as possible. Tests modeling a grid as a graph and adapting shortest-path search to a bottleneck cost.

Okta logo
Okta
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given an m x n grid where each cell holds a detection risk value. You start at the top-left cell (0, 0) and need to reach the bottom-right cell (m-1, n-1), moving one step at a time up, down, left or right.

Cells marked -1 are obstacles and cannot be entered. The risk of a path is the largest detection risk among the cells on it, including the start and end cells. Find the path that minimizes this maximum risk.

Return the minimum possible maximum detection risk. If there is no valid path from start to end, return -1.

Function Signature

def sneak_path(grid: list[list[int]]) -> int:

Rules

  • grid is a rectangular list of rows. A value of -1 marks an obstacle; every other value is that cell's risk.
  • The start and end cells count toward the path's maximum.
  • If the grid is a single cell, the path is that cell alone and the answer is its value.
  • Return -1 when the end cell cannot be reached from the start.

Constraints

  • 1 <= m, n <= 100
  • -1 <= grid[i][j] <= 1000
  • grid[0][0] != -1 and grid[m-1][n-1] != -1

Examples

Input: grid = [[0, 2, 5], [3, -1, 1], [1, 4, 2]]
Output: 4

Going right along the top row and then down the right column visits risks 0, 2, 5, 1, 2, so its maximum is 5. Going down the left column and then right along the bottom row visits 0, 3, 1, 4, 2, with maximum 4. Every route into the end cell passes through either the 5 at (0, 2) or the 4 at (2, 1), so 4 is the best possible.

Input: grid = [[0, 1], [-1, 3]]
Output: 3

The obstacle at (1, 0) leaves one route, (0, 0) -> (0, 1) -> (1, 1), with risks 0, 1, 3. Its maximum is 3.

Input: grid = [[0, -1], [-1, 0]]
Output: -1

Both neighbors of the start cell are obstacles, so the end cell cannot be reached.