Rotate Matrix Regions Around the Diagonals
Reported by candidates from Hudson River Trading's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Hudson River Trading OA reported in October 2026 hands you a matrix and says rotate the four triangles around the diagonals. It looks like a rotation problem, so most people reach for a full 90 degree rotate and wreck the diagonals on the first try. The hinted pattern is BFS, but this is really matrix simulation with index math. Constraints are small: n up to 200 and turns at most 4. If you blank on the mapping, StealthCoder is the quiet safety net running during the live OA. Here's the actual trick.
The problem
You are given an n x n integer matrix and an integer turns. The main diagonal and anti-diagonal divide the remaining cells into top, right, bottom, and left regions. One turn rotates those four regions clockwise by 90 degrees. Every non-diagonal value at (row, column) moves to (column, n - 1 - row), while every cell on either diagonal stays fixed. Apply exactly turns turns and return the resulting matrix. Function rotateMatrixOverDiagonals(matrix: int[][], turns: int) → int[][] Examples Example 1 matrix = [[1,2,3,4,5],[2,1,9,6,3],[7,0,4,8,1],[5,2,4,1,9],[6,4,3,2,1]] turns = 1 return = [[1,5,7,2,5],[4,1,0,6,2],[3,4,4,9,3],[2,2,8,1,4],[6,9,1,3,1]] The diagonal values remain in place. Every other value advances clockwise into the next triangular region. Example 2 matrix = [[1,2,3],[4,5,6],[7,8,9]] turns = 1 return = [[1,4,3],[8,5,2],[7,6,9]] The four off-diagonal values 2, 6, 8, and 4 move clockwise, while the five diagonal values stay fixed. Example 3 matrix = [[7]] turns = 3 return = [[7]] The only cell lies on both diagonals, so every turn leaves it unchanged. Constraints 1 <= n <= 200. matrix.length = matrix[i].length = n. -10^9 <= matrix[i][j] <= 10^9. 1 <= turns <= 4.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The mistake that sinks a first attempt is rotating the whole matrix and then restoring the diagonals. That breaks because diagonal cells and off-diagonal cells don't trade places cleanly. Do it the direct way instead. Copy the matrix into a result. For each cell (r, c), skip it if r == c or r + n - 1 == c. Otherwise write its value to (c, n - 1 - r) in the new grid. Repeat that for exactly turns iterations, building a fresh grid each time so you never read a cell you already overwrote. Since turns is 1 to 4, a plain loop is fine, and 4 turns returns the original. Check Example 2: 2 at (0,1) goes to (1,2), which matches. The cost is O(n^2 * turns). Don't bother with BFS. Test the 1x1 case and an even n, where the diagonals never share a center cell. StealthCoder can cover you during the live OA if the index mapping slips under pressure.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Rotate Matrix Regions Around the Diagonals 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 for the candidate who got the OA invite this morning and has 72 hours, not six months.
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 for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Rotate Matrix Regions Around the Diagonals FAQ
How hard is the Hudson River Trading rotate matrix regions problem really?+
Easy to medium. There's no fancy algorithm, just careful index math. The difficulty is not corrupting cells mid-rotation and handling the diagonals correctly. If you use a fresh grid per turn and skip diagonal cells, it's about 15 lines of code.
What's the core trick?+
Apply the given mapping directly: a non-diagonal value at (r, c) moves to (c, n-1-r). Skip any cell where r equals c or r plus c equals n-1. Write into a separate copy so reads always come from the old state, then repeat for each turn.
Do I need BFS even though it's hinted?+
No. The hint is misleading here. Nothing needs graph traversal or shortest paths. Every cell has a known destination from a formula, so a double loop over the grid does the job. Treat it as matrix simulation.
What edge cases should I test before submitting?+
Test n = 1 with any turns, since the single cell is on both diagonals. Test an even n like 2 or 4, where the diagonals don't overlap. Test turns = 4, which should return the original matrix. Also check negative values and large magnitudes.
How do I prepare in 48 hours for a matrix simulation OA like this?+
Write a few grid transforms from scratch: rotate, transpose, spiral. Practice deriving destination indices on a 3x3 by hand, then coding them. Always write to a copy. Then run Example 2 by hand against your code before trusting it.