Diagonal Traverse
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 neededProblem
Given an m x n integer matrix mat, return all of its elements collected in a **zig-zag diagonal order**.
Walk the matrix one anti-diagonal at a time (an anti-diagonal is the set of cells whose row index plus column index is constant). The direction alternates between diagonals:
- The first diagonal (the one containing only mat[0][0]) is traversed moving **up-and-to-the-right**.
- The next diagonal is traversed moving **down-and-to-the-left**.
- This alternation continues until every element has been visited.
Concretely, on diagonals where r + c is even you move upward (row decreasing, column increasing); on diagonals where r + c is odd you move downward (row increasing, column decreasing). Append the elements in the order you encounter them and return the resulting flat array.
Examples
Constraints
- `m == mat.length`
- `n == mat[i].length`
- `1 <= m, n <= 10^4`
- `1 <= m * n <= 10^4`
- `-10^5 <= mat[i][j] <= 10^5`
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.