โ† Zoho questions

Matrix Path Finding with Diagonals

Filed under Zoho

Free read 1 of 3

Initialize a queue with (start_row, start_col, distance 0) and track visited grid cells.

The question

Given an m x n grid where 0 represents an open cell and 1 represents an obstacle, find the length of the shortest path from start to target moving only diagonally (4 directions). Return -1 if no valid path exists.

Constraints

  • 1 <= m, n <= 500
  • grid[r][c] in {0, 1}

Examples

Input
grid = [[0,0,0],[0,0,0],[0,0,0]], start = [0,0], target = [2,2]
Output
2
Why
Path from (0,0) -> (1,1) -> (2,2) takes 2 diagonal moves.

Approach

  1. Initialize a queue with (start_row, start_col, distance 0) and track visited grid cells.

Tips

  • What they are testing: Test edge cases where start/target are blocked, start equals target, or destination is unreachable due to grid parity constraints.
  • Common mistake: Including orthogonal moves (up/down/left/right) when only 4 diagonal moves are allowed.

Related Zoho questions

Collected from public candidate posts and coding forums. Updated Nov 2026.