Sort the Matrix Diagonally
A medium-tier problem at 83% community acceptance, tagged with Array, Sorting, Matrix. Reported in interviews at Quora and 2 others.
Sort the Matrix Diagonally hits your OA and you blank on the trick, StealthCoder solves it in seconds, invisible to the proctor. This medium-tier problem shows up in OAs at Quora, Yandex, and Walmart Labs. The acceptance rate sits near 83%, which means most candidates who attempt it pass, but that's only because they either see the pattern or they don't. You need to understand what "diagonally" means here and how to extract, sort, and reassemble matrix data without breaking the structure. It's not hard once the shape clicks.
Companies that ask "Sort the Matrix Diagonally"
Sort the Matrix Diagonally is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share.
Get StealthCoderThe trick is recognizing that diagonals in a matrix don't move between diagonals during sort. You extract each diagonal (top-left to bottom-right traversals), sort the values within each diagonal independently, then write them back. The naive approach is to sort the entire matrix row-by-row or column-by-column, which is wrong. The constraint is that each diagonal stays its own lane. Most candidates either try to flatten and sort globally (instant fail), or they iterate row-by-row and miss the diagonal structure entirely. This is pure Array and Matrix manipulation with a Sorting pass on each diagonal. If you hit this live and freeze on the diagonal extraction logic, StealthCoder runs invisibly and surfaces the correct traversal order and reassembly pattern in real time.
Pattern tags
You know the problem.
Make sure you actually pass it.
Sort the Matrix Diagonally recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Sort the Matrix Diagonally interview FAQ
What does 'diagonally' actually mean in this problem?+
Each diagonal runs from top-left to bottom-right. Elements at positions (0,0), (1,1), (2,2) form one diagonal. Elements at (0,1), (1,2), (3,3) form another. Every diagonal is independent. You sort values within each diagonal only, never across diagonals.
Why can't I just sort the entire matrix and reshape it?+
Because the problem defines a constraint: each diagonal is a separate lane. Sorting the whole matrix breaks that structure. The values in diagonal (0,1) can't move to diagonal (0,2). That's the entire point of the problem.
How do I identify which cells belong to the same diagonal?+
Cells belong to the same diagonal if their difference (row - column) is the same. All cells where row - col = 0 are on the main diagonal. All cells where row - col = 1 are on the next diagonal below, and so on. This is the key to grouping and sorting.
Is this problem still asked at companies like Quora and Walmart Labs?+
Yes. Quora, Yandex, and Walmart Labs have all reported this problem in their OAs. The 83% acceptance rate suggests it's not a curveball, but you need to nail the diagonal extraction logic to pass.
What's the most common mistake candidates make?+
Treating diagonals as rows or columns instead of understanding the top-left to bottom-right traversal. Others flatten the matrix, sort it, and reshape, completely ignoring the diagonal constraint. The second mistake feels intuitive but is completely wrong.
Want the actual problem statement? View "Sort the Matrix Diagonally" on LeetCode →