FastPrepMatrix Escape
Problems/Matrix Escape

Matrix Escape

ByteDance logoByteDance● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a ByteDance phone screen for a full-time software engineering role. The task: you and some hazards share a grid, and you have to tell whether you can reach the edge before you are caught and, if you can, give the fewest steps. The report named the approach, one search for when the hazards reach each cell and then a search for your route that compares the two times, but gave no grid format, examples or limits.

So we chose every rule that changes an answer: the cell letters and the walls; moves of one cell up, down, left or right; a hazard time equal to the fewest steps any hazard needs; arriving at the same step as a hazard counts as caught; escaping means standing on a border cell, so a start on the border gives 0; -1 when there is no way out; and the size limits and all the examples. The resume and project questions from the same call are not part of this problem.

Our version is about 55% the same as the reported question.

Given an m x n grid grid, return the minimum number of steps you need to reach a border cell before any hazard catches you. If you cannot get out, return -1.

Each character of grid is one cell:

  • 'P' is where you start. There is exactly one.
  • 'H' is where a hazard starts. There can be any number of hazards, including none.
  • '#' is a wall. Neither you nor a hazard can enter it.
  • '.' is an open cell.

At step 0 you are on 'P' and every hazard is on its 'H' cell. In each step you move to a cell next to yours (up, down, left or right) that is not a wall. Each hazard moves the same way or stays where it is.

The hazard time of a cell is the fewest steps any hazard needs to walk there. A hazard could be on any cell it can reach in time, so you may stand on a cell after t steps only if t is smaller than that cell's hazard time. Arriving at the same step as a hazard counts as being caught. A cell no hazard can reach is always safe, and you can never enter an 'H' cell.

A border cell is any cell in the first or last row or the first or last column. You escape as soon as you stand on one, so if you start on the border, the answer is 0.

Function

minEscapeSteps(grid: String[]) → int

Examples

Example 1

grid = ["##.##","#P.H#","#.###","#....","#####"]return = 5
grid
012340##.##1#P.H#2#.###3#....4#####

The opening in the top row is 2 steps away, but the way there passes the cell between you and the hazard. The hazard reaches that cell in 1 step, the same step you would, and arriving at the same step counts as caught. Going down and then right along the bottom corridor reaches the border in 5 steps, and the hazard, which has to come through your starting cell, stays behind you.

Example 2

grid = ["#H###","#...#","#.P.#","#...H","###.#"]return = -1
grid
012340#H###1#...#2#.P.#3#...H4###.#

You can never step onto a hazard's starting cell, so the two hazard cells on the border are closed to you. The only other open border cell is in the bottom row: the hazard on the right edge gets there in 2 steps, but you need 3, so you cannot escape and the answer is -1.

Example 3

grid = ["#P#","#H#","#.#"]return = 0
grid
0120#P#1#H#2#.#

You start in the top row, which is already the border, so you escape with 0 steps, before the hazard next to you moves.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 200
  • grid[i][j] is 'P', 'H', '#' or '.'.
  • grid contains exactly one 'P'.

More ByteDance Phone Screen questions

  • Count N-Queens PlacementsPHONE SCREEN · Seen Nov 2026
  • Largest Number Below N from Reusable DigitsPHONE SCREEN · Seen Nov 2026
  • Log Analyzer: Busiest Service in a Rolling WindowPHONE SCREEN · Seen Nov 2026
  • Majority ElementPHONE SCREEN · Seen Nov 2026
  • Valid ParenthesesPHONE SCREEN · Seen Nov 2026
  • Binary Tree Target-Sum PathsPHONE SCREEN · Seen Nov 2026
  • Longest Increasing SubsequencePHONE SCREEN · Seen Nov 2026
  • Alien DictionaryPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Lexicographically Smallest Topological OrderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Maximum Number of Events That Can Be AttendedPHONE SCREEN · Seen Nov 2026

Similar questions at other companies

  • Minimum Time to Spread Through a GridAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Max Area of Island Without Changing the GridFlexport · PHONE SCREENFlexport interview questions
  • Reachable Exits in an Obstacle GridWhatNot · PHONE SCREENWhatNot interview questions
  • Process Minesweeper Game OperationsAmperity · PHONE SCREENAmperity interview questions
  • Count Islands in a Binary GridApple · PHONE SCREENApple interview questions
  • Find and Reconstruct a Binary-Matrix PathMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
  • Pacific Atlantic Water FlowAnduril · PHONE SCREENAnduril interview questions
  • Fuel-Aware Grid ReachabilityBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • House Bounds in a Ragged GridRippling · OA · PHONE SCREENRippling interview questions
  • Nearest Reachable Grid CornerSplunk · PHONE SCREENSplunk interview questions

ByteDance interview experiences

  • ByteDance TikTok USDS SWE Intern Interview: 1 Experience
  • ByteDance MLE New Grad Interviews: 1 Experience
  • ByteDance Security SRE Intern Phone Screens: 1 Experience
  • ByteDance USDS New Grad SWE Phone Screen: 1 Experience
  • ByteDance New Grad Data Engineer Video Interviews: 1 Experience
  • ByteDance Trust & Safety Research Scientist Intern: 1 Experience
All ByteDance interview questionsSee ByteDance hiring insights
Saved
class Solution {
    public int minEscapeSteps(String[] grid) {
        // Write your code here.
    }
}
Checking account…