← Meta Interview Insights

Meta·Software Engineer·Onsite - Coding / Algorithms·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

Meta SWE coding round, got a BFS graph problem where the starter code was deliberately broken and you had to fix it rather than write from scratch. Interesting twist on the usual format.

Questions Asked (1)

Q1

You're given a grid with a start cell and an end cell. A BFS implementation is provided but it's buggy because it never tracks visited nodes, so it keeps re-enqueuing cells and either loops forever or times out. Fix the bug and return the shortest path length in steps, or -1 if the end is unreachable.

Algorithms & Data Structures
Author's notes

The fix itself isn't hard once you see it.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, explain that BFS guarantees the shortest path in an unweighted grid, but only if each cell is enqueued at most once. Then, describe adding a visited set or 2D boolean array, marking cells as visited when enqueued, and returning the distance when the end cell is dequeued (or -1 if the queue empties).

Pro tip: Mention that marking visited at enqueue time (not dequeue) prevents duplicate entries and is the standard BFS pattern; also note that you can mutate the grid in-place to save memory if allowed.

1. Clarify the problem and constraints

Confirm the grid dimensions, movement directions (4-way or 8-way), and whether obstacles are represented as blocked cells. Ask if modifying the input grid is acceptable.

2. Identify the bug and its impact

Explain that without tracking visited cells, the same cell can be enqueued multiple times, leading to exponential time and potential infinite loops. This breaks BFS's O(V+E) complexity.

3. Design the fix

Add a visited data structure (e.g., a 2D boolean array or a set of coordinates). Mark a cell as visited immediately when it is enqueued, not when dequeued, to avoid duplicates.

4. Implement BFS with distance tracking

Use a queue storing (row, col, distance) or process level by level. Return the distance when the end cell is reached; if the queue empties, return -1.

5. Analyze complexity and test edge cases

State that time and space are O(rows * cols). Test cases: start equals end, unreachable end, empty grid, and single row/column.

Key Points to Mention

  • BFS explores level by level, guaranteeing shortest path in unweighted graphs.
  • Visited tracking is essential to avoid re-processing cells and infinite loops.
  • Mark visited when enqueuing, not when dequeuing, to prevent duplicates in the queue.
  • Use a queue (FIFO) and optionally store distance per cell or process level by level.
  • Time and space complexity: O(rows * cols) for both.
  • Edge cases: start == end (return 0), unreachable end (return -1), and grids with no valid path.

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