← Pinterest Interview InsightsSoftware Engineer

Pinterest·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Pinterest coding screen, one question about grid traversal. Pretty straightforward if you've done BFS/DFS problems before, but there were some wrinkles in the follow-up that slowed me down.

Questions Asked (1)

Q1

A robot vacuum moves on a 2D grid and can travel in all 8 directions (cardinal and diagonal). Starting from a given cell with no obstacles, implement a traversal to mark every reachable cell and return the count. Discuss the time and space complexity relative to grid size.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I went with BFS, which felt cleaner to reason about for this kind of flood-fill thing.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

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.

1. Clarify assumptions and constraints

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.

2. Choose traversal algorithm

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.

3. Define movement and marking

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.

4. Implement traversal with boundary checks

Use a queue or stack to process cells, checking bounds and whether a cell is already visited before adding neighbors.

5. Analyze complexity

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.

Key Points to Mention

  • Time complexity O(rows × cols) because each cell is processed once.
  • Space complexity O(rows × cols) for the visited set and queue/stack in the worst case (e.g., all cells reachable).
  • Use of a visited matrix or in-place marking to avoid revisiting cells.
  • Handling of 8 directions with boundary checks to prevent out-of-bounds access.
  • Choice between BFS and DFS: BFS avoids recursion depth issues; DFS may be simpler but risks stack overflow.
  • Edge cases: start cell already visited, grid with no obstacles but unreachable areas due to boundaries, and single-row/column grids.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.