Fewest steps to exit a grid maze with periodically open walls and paired teleporters

Quick Overview

Find the fewest steps from the start to the exit of a grid maze where walls can be entered only on steps that are multiples of k and paired teleporter pads let you jump across the grid. It tests breadth-first search over an expanded state that includes the step count modulo k.

Fewest steps to exit a grid maze with periodically open walls and paired teleporters

Company: Two Sigma

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a maze as a 2D grid of characters and an integer `k`, the period of the shifting walls. Each cell is one of the following: - `'S'`, the starting cell (exactly one). - `'E'`, the exit cell (exactly one). - `'.'`, an empty walkable cell. - `'#'`, a shifting wall. You may move onto it only when the step count after that move is a multiple of `k`. - A digit `'0'` to `'9'`, a teleporter pad. Each digit that appears is on exactly two cells, and those two pads are linked. You start on `'S'` at step 0. Each step is one action: move one cell up, down, left or right, or, if you are standing on a teleporter pad, use the step to jump to its paired pad instead. Teleporting is optional. You cannot move outside the grid. Return the minimum number of steps needed to reach `'E'`. If it cannot be reached, return `-1`. ### Function Signature ```python def min_escape_steps(grid: list[str], k: int) -> int: ``` ### Rules - `grid` is a list of strings of equal length, and `grid[r][c]` is the cell at row `r`, column `c`. Coordinates in the examples are `(row, column)`, counted from 0. - The step count is 0 on `'S'` and goes up by one with every action. The action that makes the step count `t` may enter a `'#'` cell only when `t % k == 0`. Every other cell can be entered on any step, and leaving a wall cell has no restriction of its own. - You cannot stay in place. Every step is a move to a neighbor or a teleport. - Walking onto a pad does not teleport you. The teleport is a separate step taken while standing on the pad, and you may also walk off a pad without using it. - Cells may be visited more than once. After step 0, `'S'` behaves like `'.'`. ### Constraints - `1 <= rows, cols <= 50` - `1 <= k <= 20` - `grid[i][j]` is one of `'.'`, `'#'`, `'S'`, `'E'`, or a digit `'0'` to `'9'`. - There is exactly one `'S'` and exactly one `'E'`. - Teleporter pads with the same digit come in pairs (at most 10 distinct pairs). ### Examples ```text Input: grid = ["S.#..",".##..",".#...",".#..E","....."], k = 3 Output: 7 ``` The exit at `(3, 4)` is 3 rows down and 4 columns right of the start, so no route is shorter than 7 steps. Going down to `(1, 0)` and `(2, 0)`, onto the wall at `(2, 1)` on step 3 (a multiple of 3), then through `(2, 2)`, `(2, 3)` and `(2, 4)` reaches the exit on step 7. Every 7-step route has to cross a wall, and `(2, 1)` on step 3 is the only wall entry such a route can make on a multiple of 3. ```text Input: grid = ["S.1..","#####","..1.E"], k = 4 Output: 5 ``` Walk to the pad at `(0, 2)` in 2 steps, teleport to `(2, 2)` on step 3, then walk to `(2, 3)` and reach the exit at `(2, 4)` on step 5. Without the teleporter, the best route crosses the wall row on step 4 and needs 6 steps. ```text Input: grid = ["S.#E"], k = 3 Output: 7 ``` The wall at `(0, 2)` can only be entered from `(0, 1)`, on step 3, 6, 9 and so on. Walking back and forth between `'S'` and `(0, 1)` puts you on `(0, 1)` only on odd steps, so the earliest entry is step 6 (from `(0, 1)` on step 5), and the exit follows on step 7.

Overview: Find the fewest steps from the start to the exit of a grid maze where walls can be entered only on steps that are multiples of k and paired teleporter pads let you jump across the grid. It tests breadth-first search over an expanded state that includes the step count modulo k.

Two Sigma logo
Two Sigma
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a maze as a 2D grid of characters and an integer k, the period of the shifting walls. Each cell is one of the following:

  • 'S' , the starting cell (exactly one).
  • 'E' , the exit cell (exactly one).
  • '.' , an empty walkable cell.
  • '#' , a shifting wall. You may move onto it only when the step count after that move is a multiple of k .
  • A digit '0' to '9' , a teleporter pad. Each digit that appears is on exactly two cells, and those two pads are linked.

You start on 'S' at step 0. Each step is one action: move one cell up, down, left or right, or, if you are standing on a teleporter pad, use the step to jump to its paired pad instead. Teleporting is optional. You cannot move outside the grid.

Return the minimum number of steps needed to reach 'E'. If it cannot be reached, return -1.

Function Signature

def min_escape_steps(grid: list[str], k: int) -> int:

Rules

  • grid is a list of strings of equal length, and grid[r][c] is the cell at row r , column c . Coordinates in the examples are (row, column) , counted from 0.
  • The step count is 0 on 'S' and goes up by one with every action. The action that makes the step count t may enter a '#' cell only when t % k == 0 . Every other cell can be entered on any step, and leaving a wall cell has no restriction of its own.
  • You cannot stay in place. Every step is a move to a neighbor or a teleport.
  • Walking onto a pad does not teleport you. The teleport is a separate step taken while standing on the pad, and you may also walk off a pad without using it.
  • Cells may be visited more than once. After step 0, 'S' behaves like '.' .

Constraints

  • 1 <= rows, cols <= 50
  • 1 <= k <= 20
  • grid[i][j] is one of '.' , '#' , 'S' , 'E' , or a digit '0' to '9' .
  • There is exactly one 'S' and exactly one 'E' .
  • Teleporter pads with the same digit come in pairs (at most 10 distinct pairs).

Examples

Input: grid = ["S.#..",".##..",".#...",".#..E","....."], k = 3
Output: 7

The exit at (3, 4) is 3 rows down and 4 columns right of the start, so no route is shorter than 7 steps. Going down to (1, 0) and (2, 0), onto the wall at (2, 1) on step 3 (a multiple of 3), then through (2, 2), (2, 3) and (2, 4) reaches the exit on step 7. Every 7-step route has to cross a wall, and (2, 1) on step 3 is the only wall entry such a route can make on a multiple of 3.

Input: grid = ["S.1..","#####","..1.E"], k = 4
Output: 5

Walk to the pad at (0, 2) in 2 steps, teleport to (2, 2) on step 3, then walk to (2, 3) and reach the exit at (2, 4) on step 5. Without the teleporter, the best route crosses the wall row on step 4 and needs 6 steps.

Input: grid = ["S.#E"], k = 3
Output: 7

The wall at (0, 2) can only be entered from (0, 1), on step 3, 6, 9 and so on. Walking back and forth between 'S' and (0, 1) puts you on (0, 1) only on odd steps, so the earliest entry is step 6 (from (0, 1) on step 5), and the exit follows on step 7.