← Pinterest Interview InsightsSoftware Engineer
I went with BFS, which felt cleaner to reason about for this kind of flood-fill thing.
Start by clarifying the problem: grid dimensions, start cell, and that all 8 directions are valid moves. Then describe a BFS or DFS traversal that marks visited cells and counts them, and finally analyze time and space complexity in terms of the number of cells (N = rows × cols).
Pro tip: Mention that BFS is often preferred for grid traversal because it avoids recursion depth issues and naturally handles the 8-direction expansion; also note that the visited set can be a boolean matrix or an in-place modification if allowed.
Confirm grid size, start position, and that diagonal moves are allowed. Ask whether modifying the grid in-place is acceptable or if a separate visited structure is needed.
Select BFS (queue) or DFS (stack/recursion) to explore all reachable cells. Explain why BFS is often safer for large grids due to recursion limits.
List the 8 direction offsets. For each visited cell, mark it (e.g., set to a sentinel value or use a visited matrix) and increment the count.
Use a queue or stack to process cells, checking bounds and whether a cell is already visited before adding neighbors.
State that time complexity is O(N) where N is the number of cells, since each cell is visited at most once. Space complexity is O(N) for the queue/stack and visited structure in the worst case.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.