Sort Matrix Borders
Reported by candidates from SIG's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
SIG put this one in front of candidates in September 2026, and it looks scarier than it is. Sort Matrix Borders sounds like a graph problem, but it reduces to peeling a matrix into concentric rings, sorting each ring, and writing the values back in clockwise order. No traversal tricks, no search. If you've got an OA coming, the whole job is getting the ring coordinates right, especially on thin matrices. StealthCoder sits invisibly on your screen as a safety net if you blank on the indexing mid-assessment, but this is very doable if you stay calm and build one clean helper.
The problem
Given matrix, an n x m rectangular matrix of integers, let's define its 0-border as the union of its leftmost and rightmost columns, as well as its top and bottom rows. A vector's 0-border is the vector itself. If we were to remove the matrix's 0-border, then the 0-border of the resulting matrix can be defined as the 1-border of the original matrix. We can continue this way to define the 2-border, 3-border, etc, until we reach the center of the matrix. For each valid k, your task is to sort the elements in each k-border and place them clockwise in ascending order, starting from the top-left corner. Note: You are not expected to provide the most optimal solution, but a solution with time complexity not worse than O(n * m * (n + m)) will fit within the execution time limit. Function solution(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 <= 100 1 <= matrix[i].length <= 100 Every row has the same length. -100 <= matrix[i][j] <= 100
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is separating position from value. For each layer k, generate the list of coordinates clockwise from the top-left: top row left to right, right column downward, bottom row right to left, left column upward. Read the values at those coordinates, sort them, then write them back in the same coordinate order. The hint says BFS, but it's really simulation on an array. The pitfall is degenerate layers. When the ring is a single row or a single column, you must not walk the bottom or left side again, or you'll duplicate cells and corrupt the output. Example 2 shows a single column going top to bottom. Guard with checks on top equal to bottom and left equal to right. Layers run while top <= bottom and left <= right. Cost is tiny for 100 by 100. If the coordinate generator trips you up live, StealthCoder is the hedge that gets you unstuck fast.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass SIG's OA.
SIG reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Sort Matrix Borders FAQ
How hard is Sort Matrix Borders really?+
Easy to medium. The idea is simple, sort each ring and write it back. The difficulty is purely implementation, mainly the coordinate order and the one-row or one-column edge cases. Most candidates who fail it fail on those thin layers, not on the concept.
What's the actual trick?+
Build a list of coordinates for each ring in clockwise order, pull the values, sort them, and assign them back in that same order. Keeping coordinates separate from values means you never have to reason about rotation or direction during the write phase.
Do I need BFS for this?+
No. Despite the hinted pattern, nothing here needs a queue or graph search. It's a layered matrix walk. Treat each border as a loop of cells and iterate layers from the outside in until top passes bottom or left passes right.
Which edge cases should I test first?+
Test a single row, a single column, a 2 by 2 matrix, and a matrix where the innermost layer is one row like Example 1. Also try an odd by odd matrix with a single center cell. These catch duplicate-visit bugs in the ring generator.
How do I prepare in 48 hours?+
Write the ring coordinate generator from scratch twice, then solve this with it. Practice spiral-order style traversals so the four-side walk with guards becomes automatic. Check your output against both examples before worrying about anything else.