Rat-to-bread grid path that maximizes the minimum distance to any cat

Quick Overview

Given a grid with a rat, a piece of bread, cats and obstacles, find the path from the rat to the bread whose closest approach to any cat is as large as possible, or report that no path exists. It tests grid search, distance precomputation and reasoning about a max-min path objective.

Rat-to-bread grid path that maximizes the minimum distance to any cat

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a rectangular 2D grid where each cell contains one of these characters: `'R'` (the rat's starting position), `'B'` (the bread's position), `'C'` (a cat), `'O'` (an obstacle) or `'.'` (an empty cell). The rat wants to reach the bread while staying as far away from cats as possible. The rat moves one cell at a time up, down, left or right, and it cannot move through obstacles (`'O'`) or cats (`'C'`). For a path from `'R'` to `'B'`, look at every cell on it and take the Manhattan distance from that cell to its nearest cat; the smallest of these values is the path's safety. Return the maximum safety over all paths from the rat to the bread. If there is no valid path from the rat to the bread, return `-1`. ### Function Signature ```python def max_min_cat_distance(grid: list[list[str]]) -> int: ``` ### Rules - `grid` is a list of `m` rows, each with `n` one-character strings. Cells are written as `(row, column)`, zero-indexed. - The distance between cells `(r1, c1)` and `(r2, c2)` is `|r1 - r2| + |c1 - c2|`, measured straight across the grid. Obstacles do not make it longer. - The rat's cell and the bread's cell are part of the path, so both count toward its safety. - If the grid has no cats and the bread is reachable, return `m + n`. A distance between two cells of the grid is at most `m + n - 2`, so this value cannot be confused with a real answer. ### Constraints - `1 <= m, n <= 100` - Each cell is one of `'R'`, `'B'`, `'C'`, `'O'` or `'.'`. - There is exactly one `'R'` and exactly one `'B'` in the grid. - There can be zero or more cats `'C'` and obstacles `'O'`. ### Examples ```text Input: grid = [["R",".",".",".","B"], [".",".","C",".","."], [".",".",".",".","."], [".","C",".",".","."], [".",".",".",".","."]] Output: 1 ``` The cats are at (1, 2) and (3, 1). Using only cells at distance 2 or more, the rat can reach (0, 1), (1, 0) and (2, 0) and nothing else, so 2 is impossible, while the top row reaches the bread with safety 1 (its closest cell is (0, 2), next to the cat at (1, 2)). ```text Input: grid = [["R","O","B"], ["O","O","."], [".",".","."]] Output: -1 ``` Both neighbors of the rat, (0, 1) and (1, 0), are obstacles, so the rat cannot move and the bread is unreachable. ```text Input: grid = [[".",".","C",".","."], ["R",".",".",".","B"], [".",".",".",".","."]] Output: 2 ``` The straight route along row 1 passes (1, 2), one step from the cat at (0, 2), so its safety is 1. Every path must cross column 2, and the best cell there is (2, 2) at distance 2, which a detour through row 2 uses with every other cell at distance 3 or 4.

Overview: Given a grid with a rat, a piece of bread, cats and obstacles, find the path from the rat to the bread whose closest approach to any cat is as large as possible, or report that no path exists. It tests grid search, distance precomputation and reasoning about a max-min path objective.

Google logo
Google
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
1
0

You are given a rectangular 2D grid where each cell contains one of these characters: 'R' (the rat's starting position), 'B' (the bread's position), 'C' (a cat), 'O' (an obstacle) or '.' (an empty cell).

The rat wants to reach the bread while staying as far away from cats as possible. The rat moves one cell at a time up, down, left or right, and it cannot move through obstacles ('O') or cats ('C'). For a path from 'R' to 'B', look at every cell on it and take the Manhattan distance from that cell to its nearest cat; the smallest of these values is the path's safety.

Return the maximum safety over all paths from the rat to the bread. If there is no valid path from the rat to the bread, return -1.

Function Signature

def max_min_cat_distance(grid: list[list[str]]) -> int:

Rules

  • grid is a list of m rows, each with n one-character strings. Cells are written as (row, column) , zero-indexed.
  • The distance between cells (r1, c1) and (r2, c2) is |r1 - r2| + |c1 - c2| , measured straight across the grid. Obstacles do not make it longer.
  • The rat's cell and the bread's cell are part of the path, so both count toward its safety.
  • If the grid has no cats and the bread is reachable, return m + n . A distance between two cells of the grid is at most m + n - 2 , so this value cannot be confused with a real answer.

Constraints

  • 1 <= m, n <= 100
  • Each cell is one of 'R' , 'B' , 'C' , 'O' or '.' .
  • There is exactly one 'R' and exactly one 'B' in the grid.
  • There can be zero or more cats 'C' and obstacles 'O' .

Examples

Input: grid = [["R",".",".",".","B"],
               [".",".","C",".","."],
               [".",".",".",".","."],
               [".","C",".",".","."],
               [".",".",".",".","."]]
Output: 1

The cats are at (1, 2) and (3, 1). Using only cells at distance 2 or more, the rat can reach (0, 1), (1, 0) and (2, 0) and nothing else, so 2 is impossible, while the top row reaches the bread with safety 1 (its closest cell is (0, 2), next to the cat at (1, 2)).

Input: grid = [["R","O","B"],
               ["O","O","."],
               [".",".","."]]
Output: -1

Both neighbors of the rat, (0, 1) and (1, 0), are obstacles, so the rat cannot move and the bread is unreachable.

Input: grid = [[".",".","C",".","."],
               ["R",".",".",".","B"],
               [".",".",".",".","."]]
Output: 2

The straight route along row 1 passes (1, 2), one step from the cat at (0, 2), so its safety is 1. Every path must cross column 2, and the best cell there is (2, 2) at distance 2, which a detour through row 2 uses with every other cell at distance 3 or 4.