Read a Tile Grid Along Alternating Anti-Diagonals in Zigzag Order
Last updated: November 29, 2026
Quick Overview
Given an m x n grid, return all its elements in zigzag diagonal order: walk each anti-diagonal (cells with equal row + column), alternating between up-right and down-left directions, starting upward from the top-left cell.
0
0
0 solved
Given an m x n grid, return all its elements in zigzag diagonal order: walk each anti-diagonal (cells with equal row + column), alternating between up-right and down-left directions, starting upward from the top-left cell.
An index-juggling problem that resembles the zigzag scan used in JPEG-style tile encoders. A Google interviewer uses it to see whether you find a clean invariant (r + c is constant per diagonal) rather than writing a fragile bounce simulation full of special cases.
What the Interviewer Expects
- Identifies that each anti-diagonal shares r + c
- Computes the valid row range for each diagonal directly
- Reverses direction on alternate diagonals
- Works for non-square and single-row or single-column grids
- States O(mn) time with O(1) extra space beyond output
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Produce the inverse: fill an m x n grid from a zigzag list.
- Stream the output without storing the grid, given random access to cells.
- Output each diagonal as its own list in the same alternating order.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input:
grid, m x n. - Output: list of length m*n. Diagonal
dcontains cells withr + c = d, fordfrom 0 to m+n-2. - Even
dis read bottom-to-top (row decreasing), odddtop-to-bottom....
High-Level Design
For a fixed diagonal d, the row index ranges from max(0, d - (n-1)) to min(d, m-1), and the column is d - r. So instead of simulating bounces off walls, loop over d, compute the row range, a...