Here is the interview question prompt, presented for reference.
We are given a matrix with m
rows and n
columns. For example:
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
Our goal is to traverse the matrix diagonally and return the elements in that order.
What does "diagonal" mean? It means we want to go through the matrix diagonally like this:
![Diagonal traversal visualization](https://storage.googleapis.com/algodailyrandomassets/curriculum/arrays/diagonal-traverse/problem.png)
So we start at the top left, go right, then down and left in a zig-zag.
m
rows and n
columnsm
= number of rows in matrixn
= number of columns in matrix 1 <= m, n <= 104
(1 to 10,000)1 <= m * n <= 104
-105
and 105
So in summary, we need to take a 2D matrix and figure out how to traverse it diagonally, returning the values in that order.
You can see the full challenge with visuals at this link.
Challenges • Asked over 2 years ago by Jake from AlgoDaily
This is the main discussion thread generated for Diagonal Traverse (Main Thread).