Find the fewest steps through a maze with teleporters and a fuel limit

Quick Overview

Find the fewest steps from start to exit in a grid maze where every move costs one unit of fuel, the tank holds k units, and stepping onto a teleporter allows a free jump to any other teleporter. It tests shortest-path search on a grid with zero-cost moves and working out which of the stated limits actually change the search.

Find the fewest steps through a maze with teleporters and a fuel limit

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given an `m x n` grid representing a maze. Each cell contains one of the following values: - `0`: an open path - `1`: a wall - `2`: the start position (exactly one) - `3`: the exit position (exactly one) - `4`: a teleporter You start at the cell marked `2` with a fuel capacity of `k`. Each step to an adjacent cell (up, down, left, right) costs 1 fuel. When you step onto a teleporter (`4`), you may instantly move to any other teleporter on the grid at no additional fuel cost. You may use each teleporter at most once per path. Return the minimum number of steps required to reach the exit (`3`) from the start (`2`) with at most `k` fuel. If it is impossible, return `-1`. ### Function Signature ```python def min_steps_to_exit(grid: list[list[int]], k: int) -> int: ``` ### Rules - `grid[r][c]` is the cell in row `r` and column `c`, both 0-indexed. Start, exit and teleporter cells are open, so you can walk onto and through them. - A step moves to one of the four neighboring cells that is inside the grid and is not a wall. It costs 1 fuel and counts as 1 step. - The jump between teleporters is optional. It costs no fuel and does not count as a step. - A path is valid when it uses at most `k` fuel, which is the same as taking at most `k` steps. ### Constraints - `1 <= m, n <= 100` - `m * n <= 10000` - `1 <= k <= 10000` - The grid contains exactly one cell with value `2` and exactly one cell with value `3`. - The grid contains 0 or more cells with value `4`. - `0 <= grid[i][j] <= 4` ### Examples ```text Input: grid = [[2, 0, 0, 0], [1, 1, 0, 1], [0, 0, 0, 0], [0, 1, 1, 3]], k = 10 Output: 6 ``` The only open cell in row 1 is `(1, 2)`, so the shortest path is `(0,0) -> (0,1) -> (0,2) -> (1,2) -> (2,2) -> (2,3) -> (3,3)`. It takes 6 steps, the Manhattan distance from start to exit, and 6 fuel is within `k = 10`. ```text Input: grid = [[2, 0, 1, 3], [1, 0, 1, 0], [4, 0, 1, 4], [0, 0, 0, 0]], k = 5 Output: -1 ``` The fastest route walks `(0,0) -> (0,1) -> (1,1) -> (2,1) -> (2,0)` (4 steps), jumps for free to the teleporter at `(2,3)`, then walks `(1,3) -> (0,3)`, for 6 steps in total. Walking around without the jump takes 9 steps, so no route fits in 5 fuel and the answer is `-1`. ```text Input: grid = [[2, 0, 1, 3], [1, 0, 1, 0], [4, 0, 1, 4], [0, 0, 0, 0]], k = 6 Output: 6 ``` This is the grid from the second example with one more unit of fuel. The 6-step teleporter route now uses exactly `k` fuel, which is allowed.

Overview: Find the fewest steps from start to exit in a grid maze where every move costs one unit of fuel, the tank holds k units, and stepping onto a teleporter allows a free jump to any other teleporter. It tests shortest-path search on a grid with zero-cost moves and working out which of the stated limits actually change the search.

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

You are given an m x n grid representing a maze. Each cell contains one of the following values:

  • 0 : an open path
  • 1 : a wall
  • 2 : the start position (exactly one)
  • 3 : the exit position (exactly one)
  • 4 : a teleporter

You start at the cell marked 2 with a fuel capacity of k. Each step to an adjacent cell (up, down, left, right) costs 1 fuel. When you step onto a teleporter (4), you may instantly move to any other teleporter on the grid at no additional fuel cost. You may use each teleporter at most once per path.

Return the minimum number of steps required to reach the exit (3) from the start (2) with at most k fuel. If it is impossible, return -1.

Function Signature

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

Rules

  • grid[r][c] is the cell in row r and column c , both 0-indexed. Start, exit and teleporter cells are open, so you can walk onto and through them.
  • A step moves to one of the four neighboring cells that is inside the grid and is not a wall. It costs 1 fuel and counts as 1 step.
  • The jump between teleporters is optional. It costs no fuel and does not count as a step.
  • A path is valid when it uses at most k fuel, which is the same as taking at most k steps.

Constraints

  • 1 <= m, n <= 100
  • m * n <= 10000
  • 1 <= k <= 10000
  • The grid contains exactly one cell with value 2 and exactly one cell with value 3 .
  • The grid contains 0 or more cells with value 4 .
  • 0 <= grid[i][j] <= 4

Examples

Input: grid = [[2, 0, 0, 0],
               [1, 1, 0, 1],
               [0, 0, 0, 0],
               [0, 1, 1, 3]], k = 10
Output: 6

The only open cell in row 1 is (1, 2), so the shortest path is (0,0) -> (0,1) -> (0,2) -> (1,2) -> (2,2) -> (2,3) -> (3,3). It takes 6 steps, the Manhattan distance from start to exit, and 6 fuel is within k = 10.

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

The fastest route walks (0,0) -> (0,1) -> (1,1) -> (2,1) -> (2,0) (4 steps), jumps for free to the teleporter at (2,3), then walks (1,3) -> (0,3), for 6 steps in total. Walking around without the jump takes 9 steps, so no route fits in 5 fuel and the answer is -1.

Input: grid = [[2, 0, 1, 3],
               [1, 0, 1, 0],
               [4, 0, 1, 4],
               [0, 0, 0, 0]], k = 6
Output: 6

This is the grid from the second example with one more unit of fuel. The 6-step teleporter route now uses exactly k fuel, which is allowed.