Reported October 2026
Temusimulation

Spiral Order with Obstacles

Reported by candidates from Temu's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Temu OA. Under 2s to a working solution.
Founder's read

The data structure here is a visited grid, and that's the whole game. Temu reported this one in October 2026: Spiral Order with Obstacles, a spiral walk where -1 cells block you and the walk turns clockwise when it hits a wall, an obstacle, or a cell it's already seen. It looks like a sorting problem from the tag, but it's pure simulation on a matrix. If your OA lands this week, you need a clean direction loop, not a clever trick. StealthCoder sits invisibly on screen as a safety net if you blank on the turn logic mid-assessment.

The problem

You are given a nonempty rectangular integer matrix matrix. A cell containing -1 is an obstacle; every other cell contains a value to visit.
Start at matrix[0][0] facing right. Append the current cell's value to the output. Before every move:
Continue in the current direction when the next cell is inside the matrix, is not an obstacle, and has not been visited.
Otherwise, rotate clockwise and test the next direction. Rotate at most four times for one move.
The input guarantees that matrix[0][0] is not an obstacle and that this rule reaches every non-obstacle cell exactly once. Return the values in visit order.

Function
spiralOrderWithObstacles(matrix: int[][]) → int[]

Examples
Example 1
matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
return = [1,2,3,4,8,12,11,10,9,5,6,7]
Without obstacles, the walk follows the ordinary clockwise outer layer and then the remaining inner cells.
Example 2
matrix = [[1,2,3,4],[5,6,-1,7],[8,9,10,11],[12,13,14,15]]
return = [1,2,3,4,7,11,15,14,13,12,8,5,6,9,10]
The center obstacle forces additional right turns before the walk reaches 9 and 10.
Example 3
matrix = [[1,-1,7],[2,-1,6],[3,4,5]]
return = [1,2,3,4,5,6,7]
The first attempted right move is blocked, so the walk rotates downward and reaches the right column from below.

Constraints
1 <= matrix.length and 1 <= matrix[0].length.
matrix is rectangular.
matrix.length * matrix[0].length <= 100000.
-10^9 <= matrix[r][c] <= 10^9.
-1 denotes an obstacle and is not used as a visitable value.
matrix[0][0] != -1.
The clockwise walk reaches every non-obstacle cell exactly once.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Simulate the walk. Keep a boolean visited array the same size as the matrix, a row and column pointer, and a direction index into the arrays dr = [0,1,0,-1] and dc = [1,0,-1,0]. Append the current value, mark it visited, then try to move. If the next cell is in bounds, not -1, and not visited, go. Otherwise rotate the direction index by one and test again, up to four rotations. Stop when no direction works. Time is O(rows * cols), space is O(rows * cols) for the visited grid. The common pitfall is rotating permanently before checking the straight move, or forgetting to mark the start cell visited. Another is stopping on a fixed count of cells, when obstacles change how many you visit. Loop until all four rotations fail. If you freeze on the turn order during the live OA, StealthCoder gives you the loop as a hedge.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Spiral Order with Obstacles cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Temu's OA.

Temu reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Spiral Order with Obstacles FAQ

What's the trick in Spiral Order with Obstacles?+

There's no trick beyond careful simulation. Track visited cells, keep a direction index, and rotate clockwise only when the straight move is blocked. Mark cells visited as you append them. The problem guarantees every non-obstacle cell is reached, so you just stop when no direction works.

Is this really a sorting problem?+

No. The hinted tag says sorting, but nothing gets sorted. The output order comes from the walk itself. Treat it as matrix simulation with a visited array. Don't waste time hunting for a sort step that isn't there.

How do I know when to stop the walk?+

Stop when all four rotations fail to find a valid next cell. Don't count cells against rows times columns, because obstacles reduce the total. The guarantee says the rule reaches every open cell exactly once, so a failed move means you're done.

What edge cases should I test for this Temu question?+

Try a single cell, a single row, a single column, and Example 3 where the first right move is blocked by -1. Also check a matrix with obstacles in the middle like Example 2. These catch bad rotation order and missing visited marks.

How do I prepare in 48 hours?+

Write the spiral matrix traversal from memory twice, using direction arrays and a visited grid. Then add the obstacle check as one extra condition. Trace Example 2 by hand. This variant is the standard spiral with one added blocked-cell test, so one clean pass is enough.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Temu.

OA at Temu?
Invisible during screen share
Get it