Matrix Escape
Updated
Problem statement
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[]) → intExamples
Example 1
grid = ["##.##","#P.H#","#.###","#....","#####"]return = 5The 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 = -1You 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 = 0You 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.lengthn == grid[i].length1 <= m, n <= 200grid[i][j]is'P','H','#'or'.'.gridcontains 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