Reported August 2026
ByteDancematrix

Sort Concentric Matrix Borders Clockwise

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

Input size is the first thing to check on this ByteDance OA, reported in August 2026. The matrix goes up to 200 by 200, so 40,000 cells. That sounds big, but it's tiny if you touch each cell a constant number of times. This is a matrix simulation problem, not real BFS despite the hint. You peel layers, walk each ring clockwise, sort, and write back. The danger is the traversal code, not the algorithm. If you blank on the edge cases mid-assessment, StealthCoder is the invisible safety net that can hand you a working traversal.

The problem

Given a rectangular integer matrix, divide it into concentric borders by repeatedly removing the current outer border.
For each border independently, collect its values in clockwise order starting at that border's top-left cell, sort the collected values in ascending order, and write them back along the same traversal.
Traverse a nondegenerate border across the top edge from left to right, down the right edge, across the bottom edge from right to left, and up the left edge. Visit every cell exactly once. Traverse a one-row border from left to right and a one-column border from top to bottom.
Return the resulting matrix.

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

Examples
Example 1
matrix = [[9, 7, -4, 5], [1, 6, 2, -6], [12, 20, 2, 0]]
return = [[-6, -4, 0, 1], [20, 2, 6, 2], [12, 9, 7, 5]]
The outer border is sorted and rewritten clockwise as [-6, -4, 0, 1, 2, 5, 7, 9, 12, 20]. The inner one-row border [6, 2] becomes [2, 6].
Example 2
matrix = [[3], [1], [2]]
return = [[1], [2], [3]]
The only border is one column, so it is visited from top to bottom and 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 separating the ring walk from the sort. Write one helper that returns the list of coordinates for a ring in clockwise order. Read values at those coordinates, sort them, and write them back in the same order. Total work is O(n*m log(n*m)) since each cell belongs to exactly one ring. Brute force isn't the issue here, correctness is. The pitfall is degenerate rings. When the remaining top and bottom rows are equal, or left and right columns are equal, you must not walk the bottom or left edges, or you'll visit cells twice and corrupt the output. Handle one-row and one-column rings as special cases first, then do the four-edge walk. Example 1 shows this: the inner ring is a single row [6, 2]. If you freeze on the boundary conditions during the live OA, StealthCoder is the hedge that stays invisible on screen share.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Sort Concentric Matrix Borders Clockwise 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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

ByteDance reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Sort Concentric Matrix Borders Clockwise FAQ

What's the trick in Sort Concentric Matrix Borders Clockwise?+

Build a function that lists a ring's coordinates in clockwise order, then reuse it for read and write. Sort the values between the two passes. Keeping the traversal order identical for reading and writing is what makes the answer correct, and it keeps the code short.

Is this really a BFS problem?+

No. The hinted pattern is misleading. It's matrix simulation with sorting. You process rings from the outside in using four boundary indexes (top, bottom, left, right). There's no queue or graph. Don't waste time building a BFS structure.

How do I handle single-row or single-column rings?+

Check them before the four-edge walk. If top equals bottom, walk left to right only. If left equals right, walk top to bottom only. Otherwise do the full clockwise loop. Skipping this check causes duplicate visits and wrong output.

What's the time complexity and will it pass at 200 by 200?+

Each cell is in exactly one ring, so you collect and sort at most 40,000 values total. Sorting costs O(k log k) per ring, and the overall bound is O(nm log(nm)). That's easily fast enough. Values reach 10^9 in magnitude, so use normal integer types.

How do I prepare for this in 48 hours?+

Practice spiral traversal of a matrix until the four-boundary loop is automatic. Then add the sort-and-write-back step. Test on a 1xN, an Nx1, a 2xN, and an odd-by-odd matrix. Those four shapes catch nearly every bug in this problem.

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