Sort Matrix Borders
Reported by candidates from Hudson River Trading's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Hudson River Trading reportedly put "Sort Matrix Borders" in front of candidates in September 2026, and the stated cap is O(n * m * (n + m)). That rules out anything clever with repeated full-matrix scans per element, but it's generous enough that a plain ring-by-ring simulation fits. The hinted pattern says BFS, but it's really layered traversal and sorting. If you've got the OA in a day or two, the job is simple: extract each border, sort it, write it back clockwise from the top-left. StealthCoder is the safety net if your indexing falls apart live.
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]] For matrix = [[9, 7, -4, 5], [1, 6, 2, -6], [12, 20, 2, 0]] FastPrep-authored deterministic derivation: The source image shows this example input but is cropped before its output. Applying the visible border-sorting rule gives the executable output shown here. Constraints FastPrep execution-adapter constraints (not shown in the source image): matrix is non-empty. Every row has the same positive length. Every entry is an integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is treating each k-border as a ring. For k from 0 while the ring still exists, walk the perimeter clockwise: top row left to right, right column down, bottom row right to left, left column up. Collect the values into a list, sort ascending, then walk the exact same path and write them back in order. Because the traversal order is shared, the write-back is trivial. Total work is about n * m * log, well under the stated bound. The pitfalls are all edge cases: a ring that collapses to a single row, a single column, or a single cell. If you naively run all four sides you'll double-count cells and corrupt the output. Guard the bottom row with top != bottom and the left column with left != right. If you blank on those guards mid-assessment, StealthCoder is the hedge running invisibly while you keep typing.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
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 StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Hudson River Trading's OA.
Hudson River Trading 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 to Sort Matrix Borders?+
Treat each border as a ring. Generate the clockwise coordinate list for the ring, read values along it, sort them, and write them back along the same coordinate list. Reusing one coordinate generator for both read and write removes most bugs.
How hard is this one really?+
Easy to medium on logic, annoying on indexing. There's no deep algorithm. The difficulty is correctly handling rings that degenerate into one row, one column, or one cell without visiting cells twice.
Do I need BFS for this problem?+
No. The hint says BFS, but a layer-by-layer loop with four boundary indices (top, bottom, left, right) does the job. Shrink the bounds by one after each ring and stop when they cross.
What complexity do I need to hit?+
The statement says anything not worse than O(n * m * (n + m)) fits. Ring extraction plus sorting is roughly O(n * m * log(n + m)), so you're comfortably inside the limit without optimizing further.
How do I prepare in 48 hours?+
Practice spiral matrix traversal until the four-side loop and its degenerate-case guards are automatic. Then test on a 1xN, Nx1, and odd-sized square matrix. Those three cases catch nearly every bug for this problem.