Reported July 2026
Capital Onematrix

Sort Every Matrix Border Layer

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

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

Capital One reported this one in July 2026, and the 200 by 200 limit is the first thing to read. That's 40,000 cells, so nothing fancy is needed, but a sloppy approach that re-scans the grid per layer or double-visits corners will bite you. The task is to sort each concentric border ring on its own and write it back clockwise. The hinted pattern says BFS, but this is really matrix traversal and sorting. If you blank on the corner rules mid-assessment, StealthCoder is the quiet hedge sitting on your desktop.

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. Pull values by those coordinates, sort, and assign back in the same order. Total work is about O(n*m log(n+m)), well within 40,000 cells. The real pitfall is the degenerate layers. When a layer has one row or one column, you must skip the bottom and left edges, or you'll visit cells twice and corrupt the sort. Loop layers while top <= bottom and left <= right. Check Example 3, the single column, and Example 2, the two-row case, before you submit. Don't bother with BFS, there's no graph here. If the corner logic slips under time pressure, StealthCoder can hand you a clean traversal in the live OA without anyone seeing it.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Sort Every Matrix Border Layer 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 by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

Capital One reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Sort Every Matrix Border Layer FAQ

What's the trick in Sort Every Matrix Border Layer?+

Generate the clockwise coordinates for each layer, read values at those positions, sort them, and write them back in the same order. Separating path generation from sorting keeps the code simple and makes the one-row and one-column edge cases the only thing you have to get right.

Is this really a BFS problem?+

No. The hinted pattern says BFS, but there's no graph or shortest path. It's matrix layer traversal plus sorting. Thinking in rings, not neighbors, is the fastest route to a working solution.

What edge cases break most solutions?+

Layers with a single row, a single column, or a single cell. If you always run all four edge loops, you'll double-count cells. Guard the bottom edge with more than one row and the left edge with more than one column, exactly as the statement says.

How fast does it need to be?+

With up to 200 by 200 cells, collecting and sorting each layer is plenty fast. Total cost is dominated by sorting ring values, roughly O(nm log(n+m)). You don't need anything cleverer than that.

How do I prepare for this in 48 hours?+

Practice spiral-order traversal until the four-edge loop with boundary checks is automatic. Then hand-trace the three examples, especially the single column [[3],[1],[2]]. That covers nearly every way this problem can go wrong.

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

OA at Capital One?
Invisible during screen share
Get it