How to fill a 2D array diagonally based on coordinates
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
Diagonal fills become much easier once you stop thinking about repeated boundary checks and instead think about the property that defines a diagonal. In a matrix, every cell on the same main diagonal shares the same row - col value, and every cell on the same anti-diagonal shares the same row + col value.
Use The Coordinate To Identify The Diagonal
Suppose the target coordinate is (row, col). For the top-left to bottom-right diagonal, every matching cell has the same difference between row and column.
That means you do not need to walk backward to find the start first. You can compute the diagonal key once and then scan each row:
This works for square and rectangular matrices because the column index is checked against cols on each iteration.
Fill The Anti-Diagonal
For the other direction, the invariant changes. Every cell on the same anti-diagonal has the same row + col total.
This is often easier to reason about than hand-written while loops because the traversal rule is expressed directly from the coordinate.
Test The Result On A Small Matrix
When working on matrix problems, a compact test harness is worth writing immediately. It helps you spot off-by-one errors before the real algorithm becomes more complicated.
If the coordinate is (3, 2), the filled cells should lie on the same top-left to bottom-right line as that point. Repeat the same test with fill_anti_diagonal to confirm the opposite direction.
Fill With A Sequence Instead Of One Constant
Sometimes the task is not "set every diagonal cell to 1." You may need to place values in order, such as 1, 2, 3, or copy values from another list. The easiest way is to generate the diagonal coordinates first and then fill them.
This split between "find coordinates" and "write values" is useful when the fill logic becomes more advanced, such as alternating colors on a board, copying image pixels, or writing path costs in a dynamic programming table.
Know When Traversal Order Matters
If the article or interview question only asks which cells belong to the same diagonal, the invariant-based approach is enough. If it also cares about order, be explicit about it. For example, one caller may want top-to-bottom order, while another may want the fill to start from the chosen coordinate and expand outward.
The underlying diagonal membership rule stays the same. What changes is the order in which you visit the valid coordinates. Keeping those concerns separate makes the implementation easier to adapt.
Common Pitfalls
- Forgetting that the two diagonal directions use different invariants.
- Assuming the matrix is square when the code should work for rectangular grids.
- Starting from the coordinate and filling only one direction instead of the full diagonal.
- Skipping input validation and then chasing index errors later.
- Mixing diagonal membership logic with value-generation logic in one hard-to-read loop.
Summary
- A main diagonal through a coordinate is defined by a constant
row - col. - An anti-diagonal through a coordinate is defined by a constant
row + col. - Computing the diagonal key first often leads to cleaner code than manually walking to an edge.
- Separate "which cells belong to the diagonal" from "what value should be written there."
Related reading
- How to find 3 numbers in increasing order and increasing indices in an array in linear time
- How to find a checksum of the same checksum? job-interview question
- How to find a duplicate element in an array of shuffled consecutive integers?
- How to find a triangle inside a graph?
- How to fill a tensor in C
- How to filter a dictionary according to an arbitrary condition function?
- How to find all combinations of coins when given some dollar value
- How to find all grid squares on a line?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.