Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given an m x n 2D array grid of positive integers.
Your task is to traverse grid in a zigzag pattern while skipping every alternate cell.
Zigzag pattern traversal is defined as following the below actions:
(0, 0).Note that you must skip every alternate cell during the traversal.
Return an array of integers result containing, in order, the value of the cells visited during the zigzag traversal with skips.
Example 1:
Input: grid = [[1,2],[3,4]]
Output: [1,4]
Explanation:
Example 2:
Input: grid = [[2,1],[2,1],[2,1]]
Output: [2,1,2]
Explanation:
Example 3:
Input: grid = [[1,2,3],[4,5,6],[7,8,9]]
Output: [1,3,5,7,9]
Explanation:
Constraints:
2 <= n == grid.length <= 502 <= m == grid[i].length <= 501 <= grid[i][j] <= 2500Problem summary: You are given an m x n 2D array grid of positive integers. Your task is to traverse grid in a zigzag pattern while skipping every alternate cell. Zigzag pattern traversal is defined as following the below actions: Start at the top-left cell (0, 0). Move right within a row until the end of the row is reached. Drop down to the next row, then traverse left until the beginning of the row is reached. Continue alternating between right and left traversal until every row has been traversed. Note that you must skip every alternate cell during the traversal. Return an array of integers result containing, in order, the value of the cells visited during the zigzag traversal with skips.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[[1,2],[3,4]]
[[2,1],[2,1],[2,1]]
[[1,2,3],[4,5,6],[7,8,9]]
binary-tree-zigzag-level-order-traversal)longest-zigzag-path-in-a-binary-tree)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3417: Zigzag Grid Traversal With Skip
class Solution {
public List<Integer> zigzagTraversal(int[][] grid) {
boolean ok = true;
List<Integer> ans = new ArrayList<>();
for (int i = 0; i < grid.length; ++i) {
if (i % 2 == 1) {
reverse(grid[i]);
}
for (int x : grid[i]) {
if (ok) {
ans.add(x);
}
ok = !ok;
}
}
return ans;
}
private void reverse(int[] nums) {
for (int i = 0, j = nums.length - 1; i < j; ++i, --j) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
}
}
// Accepted solution for LeetCode #3417: Zigzag Grid Traversal With Skip
func zigzagTraversal(grid [][]int) (ans []int) {
ok := true
for i, row := range grid {
if i%2 != 0 {
slices.Reverse(row)
}
for _, x := range row {
if ok {
ans = append(ans, x)
}
ok = !ok
}
}
return
}
# Accepted solution for LeetCode #3417: Zigzag Grid Traversal With Skip
class Solution:
def zigzagTraversal(self, grid: List[List[int]]) -> List[int]:
ok = True
ans = []
for i, row in enumerate(grid):
if i % 2:
row.reverse()
for x in row:
if ok:
ans.append(x)
ok = not ok
return ans
// Accepted solution for LeetCode #3417: Zigzag Grid Traversal With Skip
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3417: Zigzag Grid Traversal With Skip
// class Solution {
// public List<Integer> zigzagTraversal(int[][] grid) {
// boolean ok = true;
// List<Integer> ans = new ArrayList<>();
// for (int i = 0; i < grid.length; ++i) {
// if (i % 2 == 1) {
// reverse(grid[i]);
// }
// for (int x : grid[i]) {
// if (ok) {
// ans.add(x);
// }
// ok = !ok;
// }
// }
// return ans;
// }
//
// private void reverse(int[] nums) {
// for (int i = 0, j = nums.length - 1; i < j; ++i, --j) {
// int t = nums[i];
// nums[i] = nums[j];
// nums[j] = t;
// }
// }
// }
// Accepted solution for LeetCode #3417: Zigzag Grid Traversal With Skip
function zigzagTraversal(grid: number[][]): number[] {
const ans: number[] = [];
let ok: boolean = true;
for (let i = 0; i < grid.length; ++i) {
if (i % 2) {
grid[i].reverse();
}
for (const x of grid[i]) {
if (ok) {
ans.push(x);
}
ok = !ok;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.