BFS was the obvious move and I knew it immediately, but the problem was structured as a series of test cases building on each other, so I kept second-guessing whether my clean solution would hold up for later parts.
Model the grid as a graph where each cell is a node and edges connect adjacent non-wall cells. Use BFS to find the shortest path in terms of number of steps, since BFS explores level by level. If the target is unreachable, return -1.
Pro tip: Clarify edge cases upfront (e.g., start equals target, empty grid, no path) and mention that BFS is optimal for unweighted grids; this shows attention to detail and algorithmic maturity.
Restate the problem: find the shortest path in a 2D grid with obstacles, moving in 4 directions. Confirm that each move costs 1 and that you need the minimum number of steps.
Select BFS because it guarantees the shortest path in an unweighted graph. Explain why DFS or Dijkstra would be less efficient or unnecessary.
Describe using a queue to track cells to visit, a visited set to avoid cycles, and a distance counter or level tracking. Mention checking boundaries and walls before enqueuing neighbors.
Discuss early exit if start equals target, and returning -1 if the queue empties without reaching the target. Mention handling invalid inputs if necessary.
State that time complexity is O(R*C) since each cell is visited at most once, and space complexity is O(R*C) for the queue and visited set in the worst case.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.