Reported September 2026
TikTokmatrix

Sort Matrix Borders

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

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

The TikTok OA reported in September 2026 looks like a matrix problem, but the real test is whether you handle the degenerate layers. Sort Matrix Borders asks you to read each concentric ring clockwise, sort it, and write it back along the same path. Hinted pattern says BFS, but this is really matrix traversal plus sorting. The trap is the single-row and single-column layers, where a naive four-edge loop visits cells twice. If you blank on the boundary logic during the live OA, StealthCoder is the safety net running invisibly on your screen.

The problem

Given a rectangular integer matrix, sort the values on every concentric border layer independently in ascending order.
For each layer, visit its cells clockwise beginning at that layer's top-left cell:
Traverse the top edge from left to right.
Traverse the right edge from top to bottom, excluding the already visited top-right cell.
If the layer has more than one row, traverse the bottom edge from right to left, excluding the already visited bottom-right cell.
If the layer has more than one column, traverse the left edge from bottom to top, excluding both already visited corner cells.
Collect the values in this order, sort them in ascending order, and write them back along the same clockwise path. Continue from the outermost layer toward the center. A layer consisting of one row, one column, or one cell follows the same rule without visiting any cell twice.
Return the matrix after every layer has been sorted.

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

Examples
Example 1
matrix = [[4,1,3],[2,9,8],[7,6,5]]
return = [[1,2,3],[8,9,4],[7,6,5]]
The outer layer is read as [4,1,3,8,5,6,7,2]. Writing its sorted values [1,2,3,4,5,6,7,8] along the same path produces the returned matrix. The center value 9 forms a one-cell layer and remains unchanged.
Example 2
matrix = [[8,1,6,2],[7,4,5,3]]
return = [[1,2,3,4],[8,7,6,5]]
This two-row matrix has one layer. Its clockwise order is [8,1,6,2,3,5,4,7], so the values 1 through 8 are written in ascending order around that path.
Example 3
matrix = [[3],[1],[2]]
return = [[1],[2],[3]]
A single-column layer is visited from top to bottom exactly once, then sorted in that order.

Constraints
1 <= matrix.length <= 200
1 <= matrix[i].length <= 200
Every row has the same length.
-10^9 <= matrix[i][j] <= 10^9

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is to build the clockwise coordinate list for each layer once, then reuse it for both reading and writing. Layer k runs from top=k, left=k to bottom=R-1-k, right=C-1-k. Walk the top edge left to right. Walk the right edge from top+1 down. Only if top<bottom, walk the bottom edge right to left, skipping the corner. Only if left<right, walk the left edge bottom-1 up to top+1. Collect values, sort, write back by index. The pitfall is the guard conditions. Without them, a two-row or one-column layer double-counts cells and corrupts the output. Another pitfall is a layer that collapses to one cell in the center. Loop while top<=bottom and left<=right. Total cost is O(RC log(RC)) from the sorts, fine for 200 by 200. If the edge conditions slip away mid-assessment, StealthCoder can hand you the clean traversal while you stay in control of the submission.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Sort Matrix Borders 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 by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

TikTok reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Sort Matrix Borders FAQ

What's the trick in Sort Matrix Borders?+

Generate the list of coordinates for each ring in clockwise order, read values from those coordinates, sort them, then write back using the same list. Keeping one coordinate list avoids mismatched read and write paths, which is where most wrong answers come from.

Is this really a BFS problem?+

No. BFS is the hinted tag, but nothing here needs a queue or graph search. It's matrix traversal by layers plus sorting. Treat it as simulation: compute ring bounds, walk the edges, sort, and write back. Don't overbuild it.

Which edge cases break a naive solution?+

Single-row layers, single-column layers, and one-cell centers. Example 3 is a 3 by 1 matrix, and Example 2 is 2 rows. If you always traverse all four edges, you visit cells twice. Guard the bottom edge with top<bottom and the left edge with left<right.

What's the complexity and will it pass the constraints?+

Each cell belongs to exactly one layer, so reading and writing is O(RC). Sorting each layer adds a log factor, giving O(RC log(RC)) overall. With a 200 by 200 matrix that's at most 40000 cells, which is comfortably fine.

How do I prepare for this in 48 hours?+

Practice spiral traversal until the four edge loops and their guards are automatic. Then test by hand on a 1xN, Nx1, 2xN, and odd-by-odd matrix. Those four shapes cover nearly every bug this TikTok question can throw at you.

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

OA at TikTok?
Invisible during screen share
Get it