← Meta Interview Insights

Meta·Machine Learning Engineer·Onsite - Coding / Algorithms·Senior

Senior
May 2026

Summary

Meta ML Engineer coding round, maze problem that kept growing. Three parts, each one layered on top of the last, and by part three I was basically doing a full graph search with state tracking.

Questions Asked (3)

Q1

Given a 2D grid with a start cell, a goal cell, and walls, find any valid path from start to goal.

Algorithms & Data Structures
Author's notes

Classic BFS setup, nothing tricky.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem constraints (grid size, movement directions, whether diagonal moves are allowed) and then choose an appropriate search algorithm such as BFS or DFS. Implement the search while tracking visited cells to avoid cycles, and return the path once the goal is reached.

Pro tip: In an ML engineering interview at Meta, emphasize that BFS guarantees the shortest path in unweighted grids, which is often preferred, and discuss how this problem relates to pathfinding in reinforcement learning or robotics.

1. Clarify requirements

Ask about grid dimensions, movement rules (4-directional or 8-directional), and whether the path needs to be shortest or any valid path suffices.

2. Choose algorithm

Select BFS for shortest path or DFS for any path, considering trade-offs in time and space complexity.

3. Implement search

Use a queue (BFS) or stack (DFS) to explore cells, marking visited cells and storing parent pointers to reconstruct the path.

4. Reconstruct and return path

Once the goal is found, backtrack using parent pointers to build the path from start to goal, and return it.

5. Analyze complexity

State time and space complexity: O(R*C) for both, where R and C are grid dimensions, and discuss potential optimizations.

Key Points to Mention

  • BFS vs DFS trade-offs: BFS finds shortest path but uses more memory; DFS uses less memory but may not find shortest path.
  • Handling edge cases: start equals goal, no path exists, grid boundaries, and walls.
  • Visited set to avoid infinite loops and redundant work.
  • Path reconstruction using parent pointers or by storing the path in the queue.
  • Time and space complexity analysis: O(R*C) time and space.
  • Relevance to ML: pathfinding in reinforcement learning, robotics, or graph neural networks.

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

Q2

Extend the maze with modified movement rules, such as one-way passages or weighted cells, and find the optimal path under those constraints.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is where it got interesting.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify the modified movement rules (e.g., one-way passages, weighted cells) and the objective (e.g., minimize cost, maximize reward). Then, model the maze as a graph where nodes are cells and edges represent allowed movements with associated costs or constraints, and apply an appropriate shortest-path algorithm like Dijkstra's or A* with necessary modifications. Finally, discuss trade-offs between different algorithms and optimizations for large-scale mazes.

Pro tip: Demonstrate awareness of real-world constraints by mentioning how these modifications affect algorithm choice and performance, and propose hybrid approaches (e.g., A* with heuristics) to balance optimality and efficiency.

1. Clarify problem constraints and objective

Ask clarifying questions to understand the exact movement rules (e.g., one-way passages, weighted cells) and whether the goal is to minimize cost, maximize reward, or find any path. Confirm if weights are positive or can be negative.

2. Model as a graph

Represent the maze as a graph: each cell is a node, and allowed movements are directed edges with weights (costs) based on cell weights or passage constraints. For one-way passages, ensure edges are directed accordingly.

3. Select and adapt algorithm

Choose a shortest-path algorithm: Dijkstra's for non-negative weights, Bellman-Ford for negative weights, or A* with an admissible heuristic for efficiency. Adapt the algorithm to handle directed edges and weighted nodes (e.g., add node weights to edge costs).

4. Analyze complexity and trade-offs

Discuss time and space complexity of the chosen algorithm, and compare alternatives (e.g., BFS for unweighted, Dijkstra vs. A*). Mention potential optimizations like bidirectional search or early termination.

5. Test and validate

Outline test cases: simple mazes, cycles, negative weights (if allowed), and edge cases like unreachable targets. Validate correctness and performance.

Key Points to Mention

  • Graph representation: nodes as cells, edges as movements with weights/directions
  • Algorithm selection: Dijkstra's for non-negative weights, Bellman-Ford for negative weights, A* for heuristic-guided search
  • Handling node weights: incorporate cell weights into edge costs or use modified Dijkstra
  • Directed edges for one-way passages: ensure graph is directed and algorithm respects directionality
  • Complexity analysis: O(E + V log V) for Dijkstra with binary heap, O(VE) for Bellman-Ford
  • Trade-offs: optimality vs. efficiency, heuristic admissibility in A*, and scalability for large mazes

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

Q3

Further extend the problem to handle keys and locked doors, or find the shortest path when you're allowed to break through at most k walls.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The wall-breaking variant is basically LC 1293 and I'd seen it before, so I knew the state had to be (cell, walls_remaining) not just the cell.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as a state-space search where each state includes position, keys collected, and walls broken. Use BFS for unweighted shortest path, or 0-1 BFS/Dijkstra if breaking walls has a cost, and discuss trade-offs between state space size and optimality.

Pro tip: Clarify constraints early (grid size, number of keys, k value) to choose the right algorithm; mention that for small k, BFS with state (r, c, keys, broken) is fine, but for large k, consider A* with a heuristic or bidirectional search to prune the state space.

1. Clarify problem variants and constraints

Ask whether keys are reusable, if multiple keys of same type exist, and the maximum grid size, number of keys, and k. This determines whether BFS, 0-1 BFS, or Dijkstra is appropriate.

2. Define the state representation

Encode state as (row, col, key_bitmask, walls_broken). Use a bitmask for keys if the number of key types is small (≤20). For walls broken, if k is small, include it as a dimension; otherwise, treat it as a cost.

3. Choose the search algorithm

If all moves have equal cost, use BFS. If breaking a wall costs 1 and moving costs 0, use 0-1 BFS with a deque. If costs vary, use Dijkstra. For large state spaces, consider A* with a heuristic like Manhattan distance to the goal.

4. Handle keys and doors

When encountering a door, only pass if the corresponding key is in the bitmask. When picking up a key, update the bitmask. Ensure visited states include the key bitmask to avoid revisiting with different keys.

5. Optimize and discuss trade-offs

Prune states where walls_broken > k. Discuss time/space complexity: O(R*C*2^K*(k+1)) for BFS. Mention alternatives like bidirectional BFS or A* for performance, and trade-offs between optimality and speed.

Key Points to Mention

  • State-space search with augmented state (position, keys, walls broken)
  • BFS vs 0-1 BFS vs Dijkstra based on edge weights
  • Bitmask for key representation to keep state compact
  • Visited set must include key bitmask and walls broken count
  • Pruning when walls broken exceeds k
  • Time and space complexity analysis and potential optimizations (A*, bidirectional search)

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