← Meta Interview Insights

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

Intermediate
Jun 2026

Summary

Meta SWE coding round, one algorithmic problem the whole time. The maze rolling-ball thing looks like a BFS/DFS problem but the stopping condition trips you up if you're not careful.

Questions Asked (1)

Q1

Given a 2D grid where 0 is open and 1 is a wall, a ball starts at a given cell and rolls in a chosen direction until it hits a wall or boundary. You can only pick a new direction once the ball stops. Determine whether the ball can stop exactly on a target cell.

Algorithms & Data Structures
Author's notes

The part that got me was treating the stopping point correctly.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as a graph where each cell is a node and edges represent rolling in one of the four directions until hitting a wall. Use BFS or DFS to explore all reachable stopping positions from the start, and check if the target is among them. Precompute the next stopping cell for each direction at each cell to avoid redundant simulation.

Pro tip: Clarify that the ball must stop exactly on the target, not just pass over it. Also, mention that you can optimize by precomputing the next stop for each cell and direction in O(mn) time, reducing the overall complexity to O(mn).

1. Clarify problem constraints and edge cases

Confirm the grid dimensions, whether the start and target are guaranteed to be open, and if the ball can stop at the start if it's the target. Discuss boundary conditions and if the ball can get stuck.

2. Define state and transitions

Each state is a cell where the ball can stop. From a state, rolling in a direction leads to a new state (the cell before hitting a wall or boundary). This forms a directed graph.

3. Choose traversal algorithm

Use BFS or DFS to explore all reachable states from the start. BFS is natural for finding if a target is reachable, but DFS works too. Track visited states to avoid cycles.

4. Optimize with precomputation

Precompute for each cell and direction the next stopping cell. This can be done by scanning rows and columns to find the nearest wall in each direction, reducing simulation time.

5. Analyze complexity and test

Time complexity is O(mn) with precomputation, otherwise O(mn * max(m,n)) if simulating each roll. Space is O(mn) for visited and precomputed arrays. Walk through a small example to verify.

Key Points to Mention

  • Graph modeling: cells as nodes, rolls as edges
  • BFS/DFS for reachability
  • Visited set to avoid infinite loops
  • Precomputation of next stop for each direction
  • Time and space complexity analysis
  • Edge cases: start equals target, target unreachable, ball cannot move

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