Reported September 2026
ByteDancematrix

Spiral Matrix Traversal

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

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

The edge case that kills most spiral solutions is the single row or single column left over after you peel a ring. ByteDance candidates reported this Spiral Matrix Traversal OA in September 2026, and the tests are built to catch exactly that. The task is simple on paper: return a non-empty matrix in clockwise order from the top-left. The code is where people double-count cells. If you blank on the boundary logic, StealthCoder runs invisibly during the live OA and gives you a working solution as a safety net. Still, you can learn the trick in ten minutes.

The problem

You are given a non-empty rectangular integer matrix.
Return all elements in clockwise spiral order, beginning at the top-left corner. Traverse the top edge, right edge, bottom edge in reverse, and left edge in reverse, then repeat on the remaining inner rectangle.

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

Examples
Example 1
matrix = [[1,2,3],[4,5,6],[7,8,9]]
return = [1,2,3,6,9,8,7,4,5]
The outer ring is 1,2,3,6,9,8,7,4; the remaining center is 5.
Example 2
matrix = [[1,2,3,4],[5,6,7,8]]
return = [1,2,3,4,8,7,6,5]
The two-row matrix is consumed entirely by its outer ring.

Constraints
1 <= matrix.length, matrix[i].length <= 1000
All rows have the same length.
The matrix contains at most 10^5 elements.
-10^9 <= matrix[i][j] <= 10^9

Reported by candidates. Source: FastPrep

Pattern and pitfall

The hint says breadth-first-search, but this is simulation with four shrinking boundaries: top, bottom, left, right. Walk the top row left to right, then top++. Walk the right column down, then right--. Now the pitfall: before walking the bottom row and left column, check that top <= bottom and left <= right. Skip that check and a single leftover row or column gets traversed twice. Example 2, the two-row matrix, is the case where the outer ring eats everything and nothing should remain. Example 1 leaves the center 5. Loop while top <= bottom and left <= right. That's O(m*n) time and O(1) extra space beyond the output. A visited-array version also works and avoids the boundary checks, but it costs extra memory. With up to 10^5 elements either one fits. If the boundary checks slip under pressure, StealthCoder is the hedge on the live OA. It reads the problem and hands you the guarded version.

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 Matrix Traversal 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

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as spiral matrix. If you have time before the OA, drill that.

⏵ The honest play

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

ByteDance 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 Matrix Traversal FAQ

How hard is the ByteDance Spiral Matrix Traversal OA really?+

It's easy on concept and unforgiving on details. No clever algorithm is needed. The difficulty is off-by-one handling when the matrix is a single row, a single column, or has an odd center. Most failures come from those cases, not from the main loop.

What's the trick to avoid duplicate elements?+

Re-check the boundaries after finishing the top row and right column. Only walk the bottom row if top <= bottom, and only walk the left column if left <= right. Without those guards, a leftover single row or column gets read twice on the way back.

Is BFS actually the right pattern here?+

No. The hinted BFS label is misleading. This is a boundary simulation. You don't need a queue. Four pointers shrinking inward, or a direction-change approach with a visited marker, covers it cleanly in O(m*n) time.

Which test cases should I run before submitting?+

Run a 1x1 matrix, a single row, a single column, a 2-row wide matrix like Example 2, and a square odd-sized matrix like Example 1. Those five cases expose almost every boundary bug. Also try a tall matrix with more rows than columns.

How do I prepare in 48 hours?+

Write the four-pointer spiral from scratch twice without looking. Then trace the 2x4 example by hand and watch which guard stops the extra pass. Do the same for a 3x1 column. That covers the pattern. Don't spend time on fancier matrix problems.

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

OA at ByteDance?
Invisible during screen share
Get it