Ctrl K

Zigzag Diagonal Sweep

Medium
A scanning robot reads a rectangular m × n table grid diagonal by diagonal, snaking back and forth. It starts in the top-left corner and moves along the anti-diagonals (all cells of one anti-diagonal share the same row + column). The first anti-diagonal is read going up and to the right, the second going down and to the left, the third up again, and so on until the robot reaches the bottom-right corner. Return the values in the order the robot reads them.
Example 1
grid=[[5, 1, 9], [2, 8, 4]] → [5, 1, 2, 8, 9, 4]

First 5, then down-left: 1, 2. Then up-right: 8, 9. The last diagonal goes down and holds just one cell, 4.

Example 2
grid=[[3], [7], [6]] → [3, 7, 6]

With a single column every diagonal is one cell, so the walk simply goes top to bottom.

Example 3
grid=[[-4, 10]] → [-4, 10]
Constraints
  • 1 ≤ m, n
  • m · n ≤ 10⁴
  • all rows have the same length
  • -10⁵ ≤ grid[i][j] ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSpiral Readout MediumSpiral-Numbered Square MediumNext Step of a Cell Colony MediumMatch-Three Cascade MediumOutward Spiral Walk Medium
loading Python…
Input
grid=[[5, 1, 9], [2, 8, 4]]
Expected
[5, 1, 2, 8, 9, 4]

Run checks the first 3 tests in your browser. Submit checks all 65 tests on the server, hidden ones included.