I went straight to BFS which was the right call for shortest path, but I spent probably too long explaining it before writing any code.
Model the maze as a graph where each cell is a node and edges connect adjacent non-wall cells. Use BFS to find the shortest path from start to target, as BFS guarantees the minimum number of steps in an unweighted grid. If the target is unreachable, BFS will exhaust all reachable cells and return -1.
Pro tip: In interviews, always clarify edge cases upfront (e.g., start equals target, empty grid, no path) and discuss time/space complexity. Mention that BFS is optimal for unweighted graphs, but if the grid is huge, consider bidirectional BFS or A* with Manhattan distance heuristic to reduce search space.
Ask about grid dimensions, movement rules, whether diagonal moves are allowed, and if the start or target can be on walls. Confirm that you need the minimum number of steps, not just reachability.
Explain that BFS is ideal because it explores level by level, guaranteeing the shortest path in an unweighted grid. Mention alternatives like DFS (not optimal for shortest path) and A* (if heuristic is available).
Describe using a queue to store cells and their distances, a visited set to avoid cycles, and directional offsets for the four moves. Initialize with the start cell and distance 0.
Check if start equals target (return 0), if start or target is a wall (return -1), and if the queue empties without finding the target (return -1). Also consider grid boundaries.
State that time complexity is O(R*C) since each cell is visited once, and space is O(R*C) for the queue and visited set. Discuss potential optimizations like bidirectional BFS or early exit when target is found.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.