Sort the Matrix Diagonally

MediumMatrixsimulation

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

A matrix diagonal runs down and to the right: it starts at a cell in the top row or the leftmost column and continues through (i + 1, j + 1) until it leaves the matrix. Given an m x n integer matrix mat, sort the values along every such diagonal in ascending order (smallest at the top-left end) and return the resulting matrix.

Examples

Input: mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]]
Output: [[1,1,1,1],[1,2,2,2],[1,2,3,3]]
For instance the main diagonal `3, 2, 1` becomes `1, 2, 3`.
Input: mat = [[4,2],[3,1]]
Output: [[1,2],[3,4]]
The main diagonal `4, 1` becomes `1, 4`; the other diagonals have one cell each.

Constraints

  • 1 <= m, n <= 100
  • 1 <= mat[i][j] <= 100

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More simulation problems