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.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

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
Diagonal Traversal
Index Invariants
Matrix Traversal
Simulation
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample Answer
Requirements
  • Input: grid, m x n.
  • Output: list of length m*n. Diagonal d contains cells with r + c = d, for d from 0 to m+n-2.
  • Even d is read bottom-to-top (row decreasing), odd d top-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...


Submit Your Answer
Markdown supported