Shortest Path in Binary Matrix (BFS)

Data Structures
Medium
LinkedIn
76.7K views

Given an $n \times n$ binary matrix, return the length of the shortest clear path from the top-left to the bottom-right corner. Use BFS.

Why Interviewers Ask This

LinkedIn asks this to evaluate a candidate's mastery of graph traversal algorithms in constrained environments. They specifically test if you recognize that unweighted shortest path problems require Breadth-First Search (BFS) rather than Dijkstra or DFS. The binary matrix constraint checks your ability to handle grid-based adjacency and boundary conditions efficiently.

How to Answer This Question

1. Clarify the problem constraints immediately, noting that movement is allowed in all 8 directions (horizontal, vertical, diagonal). 2. Propose BFS as the optimal approach because it guarantees the shortest path in an unweighted graph by exploring layer by layer. 3. Define your queue initialization with the starting coordinate and distance count of one, ensuring you check for immediate edge cases like blocked start or end points. 4. Implement the neighbor iteration logic carefully, validating bounds and checking if cells are zero (blocked) before adding them to the queue. 5. Return the distance upon reaching the target or -1 if the queue empties, demonstrating robust error handling consistent with LinkedIn's focus on production-ready code quality.

Key Points to Cover

  • Explicitly state why BFS is chosen over DFS or Dijkstra for unweighted grids
  • Demonstrate handling of the 8-directional movement requirement correctly
  • Show awareness of boundary checks and obstacle validation during traversal
  • Explain the trade-off between modifying input vs. using extra space for visited tracking
  • Confirm understanding of O(N^2) time and space complexity implications

Sample Answer

To solve the shortest clear path in a binary matrix, I would use Breadth-First Search, which is ideal for finding the minimum steps in an unweighted grid. First, I'd validate edge cases: if the matrix is empty, or if the start or end cell is blocked, I'd return -1 immediately. Next, I initialize a queue with the top-left coordinate and set the current path length to 1. I also maintain a visited structure, either by modifying the input matrix to mark visited cells as 0 or using a separate boolean matrix, to prevent cycles. Then, I process the queue level by level. For each cell, I explore all eight possible neighbors. If a neighbor is within bounds, is a clear path (value 1), and hasn't been visited, I add it to the queue with an incremented distance. This ensures we find the destination via the shortest route first. Once the bottom-right corner is dequeued, I return the accumulated distance. If the queue empties without reaching the target, it means no valid path exists, so I return -1. This approach runs in O(N^2) time and space, which is optimal since we may need to visit every cell once. This method aligns well with engineering standards at companies like LinkedIn where efficiency and correctness are paramount.

Common Mistakes to Avoid

  • Using DFS instead of BFS, which finds any path but not necessarily the shortest one
  • Forgetting to check the start or end node validity before beginning the search
  • Failing to track visited nodes, leading to infinite loops in cyclic paths
  • Implementing only 4-directional movement instead of including diagonals
  • Not returning -1 explicitly when no path exists, causing runtime errors

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 154 Data Structures questionsBrowse all 26 LinkedIn questions