Implement BFS shortest path in grid
Problem Description
Implement a function that calculates and returns the length of the shortest path in an unweighted 2D grid. The path must start at the top-left cell (0, 0) and end at the bottom-right cell (m-1, n-1).
The grid consists of cells represented by characters:
'0'denotes an open cell, through which movement is allowed.'1'denotes a wall, which blocks movement.
Movement is restricted to four cardinal directions: up, down, left, and right. Diagonal moves are not permitted in the primary problem.
If the starting cell (0, 0) or the ending cell (m-1, n-1) is blocked by a wall ('1'), or if no valid path exists between the start and end cells, the function should return -1.
Deliverables
Your solution should include:
- The algorithm used (specifically Breadth-First Search, BFS).
- An explanation of why BFS is the appropriate algorithm for this problem.
- A detailed analysis of the time and space complexity of your solution.
- A discussion of relevant edge cases, including:
- Single-cell grids (e.g., 1x1).
- Very large grids (up to 10^5 cells).
- Scenarios where multiple shortest paths of the same length exist.
Optional Extension
Discuss how the algorithm would need to change to support diagonal moves (eight directions of movement). Explain the modifications required.
AI interview prep
Core dilemma
Efficiently exploring all reachable cells in an unweighted grid to guarantee finding the *shortest* path, while avoiding cycles and redundant computations, especially under memory constraints for large grids.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement BFS shortest path in grid
Problem Description
Implement a function that calculates and returns the length of the shortest path in an unweighted 2D grid. The path must start at the top-left cell (0, 0) and end at the bottom-right cell (m-1, n-1).
The grid consists of cells represented by characters:
'0'denotes an open cell, through which movement is allowed.'1'denotes a wall, which blocks movement.
Movement is restricted to four cardinal directions: up, down, left, and right. Diagonal moves are not permitted in the primary problem.
If the starting cell (0, 0) or the ending cell (m-1, n-1) is blocked by a wall ('1'), or if no valid path exists between the start and end cells, the function should return -1.
Deliverables
Your solution should include:
- The algorithm used (specifically Breadth-First Search, BFS).
- An explanation of why BFS is the appropriate algorithm for this problem.
- A detailed analysis of the time and space complexity of your solution.
- A discussion of relevant edge cases, including:
- Single-cell grids (e.g., 1x1).
- Very large grids (up to 10^5 cells).
- Scenarios where multiple shortest paths of the same length exist.
Optional Extension
Discuss how the algorithm would need to change to support diagonal moves (eight directions of movement). Explain the modifications required.