Grokking Meta Coding Interview
Ask Author
Back to course home

0% completed

Solution: Diagonal Traverse

Problem Statement

Given a 2D matrix of size m x n, return a 1D array containing elements of the matrix in the diagonal order.

Example 1:

  • Input: `matrix =
[[1, 2, 3, 4],
 [5, 6, 7, 8],
 [9, 10, 11, 12],
 [13, 14, 15, 16]]
  • Expected Output: [1, 2, 5, 9, 6, 3, 4, 7, 10, 13, 14, 11, 8, 12, 15, 16]
  • Justification: Traversal begins at 1, moves to 2, diagonally down to 5, and so on.

Example 2:

  • Input: matrix =
[[1,5],
 [3,6]]
  • Expected Output: [1,5,3,6]

.....

.....

.....

Like the course? Get enrolled and start learning!