Trace an Eight-Direction Robot Until Blocked
Updated
Problem statement
You are given an integer matrix grid. Each cell is either open, marked 0, or a blocker, marked -1. A robot starts at start, given as [row, column]. That cell is open.
The array moves lists the robot's steps in order. Each step [dr, dc] moves the robot one cell in one of eight directions: dr changes the row and dc changes the column. Both dr and dc are in {-1, 0, 1}, and they are not both zero. So the robot can move up, down, left, right or diagonally.
Mark the starting cell and every open cell the robot enters with 1. Do the moves in order. If a move would take the robot off the matrix or onto a blocker, stop right there and ignore all the moves after it. The robot may enter a cell that is already marked 1.
Return the final matrix. Blockers stay -1.
Function
traceRobot(grid: int[][], start: int[], moves: int[][]) → int[][]Examples
Example 1
grid = [[0,0,0],[0,-1,0],[0,0,0]]start = [2,0]moves = [[-1,0],[0,1],[0,1],[1,1]]return = [[0,0,0],[1,-1,0],[1,0,0]]The first move marks [1,0]. The next move would enter the blocker at [1,1], so the robot stops.
Example 2
grid = [[0,0],[0,0]]start = [0,0]moves = [[0,1],[1,0],[0,-1],[-1,0]]return = [[1,1],[1,1]]The four moves visit every cell and return to the start.
Constraints
1 <= grid.length, grid[i].length <= 200.- All rows have the same length. At the start, every cell is
0or-1. startis an open cell.0 <= moves.length <= 200000.- Every move is one of the eight directions described above.
More Pinterest Phone Screen questions
- Shortest Pin Path Through TopicsPHONE SCREEN · Seen Nov 2026
- Top K Pins with Insert and QueryPHONE SCREEN · Seen Nov 2026
- Pins Violation Log QueriesPHONE SCREEN · Seen Nov 2026
- Sparse Matrix Addition and MultiplicationPHONE SCREEN · Seen Nov 2026
- Minimum Bus Routes to a DestinationPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Delete and Compact a Forest SubtreePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Optimal Account BalancingPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Aggregate Matching IPv4 Addresses Across ResponsesPHONE SCREEN · Seen Oct 2026
- Escape Room LeaderboardPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Shortest Way to Form a Target StringPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Validate a Chess Bishop, Rook, or Queen MoveGlean · PHONE SCREEN
- Excel Training Data and Model Response AuditMicrosoft · PHONE SCREEN
- Count Connect-Four Scoring LinesBloomberg LP · PHONE SCREEN
- Mark a Queen’s Attack BoardMotive · PHONE SCREEN
- Spiral Order with ObstaclesTemu · PHONE SCREEN
- Incremental Hard Attention with a KV CacheOpenAI · PHONE SCREEN · ONSITE INTERVIEW
- Spiral Matrix TraversalByteDance · PHONE SCREEN
- S-Shaped Matrix TraversalFox Corporation · PHONE SCREEN
- Zigzag Board Adjacent SwapsUber · PHONE SCREEN
- Longest Consecutive Same-Color LineGoogle · PHONE SCREEN