Reach a snack from your desk without passing next to a boss

Quick Overview

Decide whether you can walk from a desk cell to any snack cell in an office grid while avoiding walls and every cell that holds a boss or sits next to one. Tests turning a movement rule into a set of blocked cells and checking reachability on a grid.

Reach a snack from your desk without passing next to a boss

Company: Glean

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given a 2D `grid` representing an office layout. Each cell holds one of these characters: - `'D'` is your desk, the starting position. - `'S'` is a snack. - `'B'` is a boss. - `'#'` is a wall, which cannot be entered. - `'.'` is empty floor. You move between cells that share an edge (up, down, left, right). Any cell that is a boss, or is orthogonally adjacent to a boss, may not be visited. This restriction also applies to the desk and snack cells themselves. Return `True` if there is a path from your desk to any snack that visits only allowed cells. Otherwise return `False`. ### Function Signature ```python def can_reach_snack(grid: list[list[str]]) -> bool: ``` ### Rules - `grid` is rectangular, and each entry is a one-character string. - A cell is forbidden if it is a `'B'` or shares an edge with a `'B'`. Cells that touch a boss only diagonally are allowed. - If the desk is forbidden, the answer is `False`. - If the grid has no snack, the answer is `False`. ### Constraints - `1 <= len(grid), len(grid[i]) <= 100` - Each `grid[i][j]` is one of `'D'`, `'S'`, `'B'`, `'#'` or `'.'`. - There is exactly one `'D'` in the grid. ### Examples ```text Input: grid = [["D", ".", ".", "."], [".", "#", "#", "."], [".", ".", "B", "."], [".", ".", ".", "S"]] Output: False ``` The boss at `(2, 2)` forbids itself and `(1, 2)`, `(3, 2)`, `(2, 1)` and `(2, 3)`. The snack at `(3, 3)` touches the boss only diagonally, so it is allowed, but its only two neighbors, `(2, 3)` and `(3, 2)`, are both forbidden. No path can reach it. ```text Input: grid = [["D", ".", "S"], [".", ".", "."], [".", ".", "."]] Output: True ``` There is no boss, so walking right two cells reaches the snack. ```text Input: grid = [["D", ".", "S"], ["B", ".", "."]] Output: False ``` The boss at `(1, 0)` is directly below the desk, so the desk is forbidden and you cannot start. Without that rule the top row would be a valid route.

Overview: Decide whether you can walk from a desk cell to any snack cell in an office grid while avoiding walls and every cell that holds a boss or sits next to one. Tests turning a movement rule into a set of blocked cells and checking reachability on a grid.

Glean logo
Glean
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given a 2D grid representing an office layout. Each cell holds one of these characters:

  • 'D' is your desk, the starting position.
  • 'S' is a snack.
  • 'B' is a boss.
  • '#' is a wall, which cannot be entered.
  • '.' is empty floor.

You move between cells that share an edge (up, down, left, right). Any cell that is a boss, or is orthogonally adjacent to a boss, may not be visited. This restriction also applies to the desk and snack cells themselves.

Return True if there is a path from your desk to any snack that visits only allowed cells. Otherwise return False.

Function Signature

def can_reach_snack(grid: list[list[str]]) -> bool:

Rules

  • grid is rectangular, and each entry is a one-character string.
  • A cell is forbidden if it is a 'B' or shares an edge with a 'B' . Cells that touch a boss only diagonally are allowed.
  • If the desk is forbidden, the answer is False .
  • If the grid has no snack, the answer is False .

Constraints

  • 1 <= len(grid), len(grid[i]) <= 100
  • Each grid[i][j] is one of 'D' , 'S' , 'B' , '#' or '.' .
  • There is exactly one 'D' in the grid.

Examples

Input: grid = [["D", ".", ".", "."], [".", "#", "#", "."], [".", ".", "B", "."], [".", ".", ".", "S"]]
Output: False

The boss at (2, 2) forbids itself and (1, 2), (3, 2), (2, 1) and (2, 3). The snack at (3, 3) touches the boss only diagonally, so it is allowed, but its only two neighbors, (2, 3) and (3, 2), are both forbidden. No path can reach it.

Input: grid = [["D", ".", "S"], [".", ".", "."], [".", ".", "."]]
Output: True

There is no boss, so walking right two cells reaches the snack.

Input: grid = [["D", ".", "S"], ["B", ".", "."]]
Output: False

The boss at (1, 0) is directly below the desk, so the desk is forbidden and you cannot start. Without that rule the top row would be a valid route.