FastPrepSpiral Matrix with Obstacles
Problems/Spiral Matrix with Obstacles

Spiral Matrix with Obstacles

Temu logoTemu● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Temu onsite interview. The report describes a task on a rectangular spiral matrix and a follow-up where cells containing -1 force a turn. It does not give the exact rules for movement, output, and stopping. Our version is about 85% the same as the reported question.

You are given a nonempty rectangular integer matrix grid. Every cell initially contains either 0, meaning it is fillable, or -1, meaning it is an obstacle.

Starting at the top-left cell and moving right, write consecutive positive integers into fillable cells. Before each move, inspect the next cell:

  • If it is inside the matrix, is not an obstacle, and has not yet been numbered, move into it.
  • Otherwise, rotate clockwise and inspect the next direction. Continue rotating, up to all four directions, until a valid move is available.

Obstacle cells remain -1. The input guarantees that the top-left cell is fillable and that this clockwise right-turn walk reaches every fillable cell. Stop after numbering every fillable cell, then return grid.

Function

fillSpiralWithObstacles(grid: int[][]) → int[][]

Examples

Example 1

grid = [[0,0,0,0],[0,0,0,0],[0,0,0,0]]return = [[1,2,3,4],[10,11,12,5],[9,8,7,6]]
grid
0123000001000020000
→
return
0123012341101112529876
Moved or new

With no obstacles, the matrix is numbered in the usual clockwise spiral order.

Example 2

grid = [[0,0,0,0],[0,0,-1,0],[0,0,0,0],[0,0,0,0]]return = [[1,2,3,4],[12,13,-1,5],[11,14,15,6],[10,9,8,7]]
grid
012300000100-102000030000
→
return
01230123411213-1521114156310987
Moved or new

The obstacle remains -1. Near the center, the traversal may reject several directions before finding the next open, unnumbered cell.

Example 3

grid = [[0,-1,0],[0,-1,0],[0,0,0]]return = [[1,-1,7],[2,-1,6],[3,4,5]]
grid
01200-1010-102000
→
return
01201-1712-162345
Moved or new

The first move to the right is blocked, so the traversal rotates downward and later reaches the right column from below.

Constraints

  • 1 <= grid.length and 1 <= grid[0].length.
  • grid is rectangular.
  • grid.length * grid[0].length <= 100000.
  • Every initial cell is either 0 or -1.
  • grid[0][0] == 0.
  • The clockwise right-turn walk reaches every fillable cell.

More Temu interview questions

  • Detect a Straight FlushPHONE SCREEN · Seen Nov 2026
  • Next Taller Building to the RightPHONE SCREEN · Seen Nov 2026
  • SubsetsPHONE SCREEN · Seen Nov 2026
  • Spiral Order with ObstaclesPHONE SCREEN · Seen Oct 2026
  • Generate Spiral MatrixPHONE SCREEN · Seen Aug 2026
  • Merge Two Sorted Collections Without DuplicatesPHONE SCREEN · Seen Aug 2026

Similar questions at other companies

  • Explore an Unknown Grid and Find a PathLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • Rectangle Point Coverage CountsOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
  • Spiral MatrixNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
  • Tic-Tac-Toe Move WinnersByteDance · ONSITE INTERVIEWByteDance interview questions
  • Trace Every Water Drop to Its Resting DestinationGoogle · ONSITE INTERVIEWGoogle interview questions
  • Stochastic Gradient Descent with MomentumMeta · ONSITE INTERVIEWMeta interview questions
  • Cyclic Fuel Command SimulationWaymo · ONSITE INTERVIEWWaymo interview questions
  • Generate a Spiral MatrixMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Minesweeper Board UpdateUber · ONSITE INTERVIEWUber interview questions
  • Stateful Neuron Matrix UpdateScale AI · ONSITE INTERVIEWScale AI interview questions
All Temu interview questionsSee Temu hiring insights
Saved
class Solution {
    public int[][] fillSpiralWithObstacles(int[][] grid) {
        // Write your code here.
    }
}
Checking account…