← Pinterest Interview Insights
Clarify the grid boundaries and whether the obstacle is passable, then choose BFS or DFS to traverse the 8-directional neighbors. Use a visited set to avoid revisiting cells, and return the set of reachable cells excluding the start and obstacle.
Pro tip: Mention that BFS is preferable for finding shortest paths, but since the question only asks for reachable cells, DFS is equally valid and may be simpler to implement iteratively. Also, explicitly handle edge cases like the start being the obstacle or out of bounds.
Ask about grid dimensions, whether the obstacle is passable, and if the start can be the obstacle. Confirm that movement is allowed in all 8 directions and that cells are considered reachable if there is a path avoiding the obstacle.
Decide between BFS (queue) or DFS (stack/recursion). Both work; BFS naturally explores level by level, while DFS may use less memory for deep paths. Mention that the choice doesn't affect correctness here.
Initialize a queue or stack with the start cell and a visited set containing the start. While the structure is not empty, pop a cell, add it to the reachable set, and for each of the 8 neighbors, if it's within bounds, not the obstacle, and not visited, add it to the structure and mark visited.
Check if the start is the obstacle or out of bounds; if so, return an empty set. Otherwise, return the visited set (which includes the start) or exclude the start if specified. Ensure the obstacle is never added.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.