← Meta Interview Insights

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

Senior
Jun 2026

Summary

Meta infrastructure interview that was basically a maze problem with four layers of increasing complexity. Each sub-problem built on the last, which sounds clean in theory but in practice you're juggling BFS state spaces and TSP DP at the same time. Pretty demanding for a coding round.

Questions Asked (5)

Q1

Given a 2D grid maze with a start and exit, find the shortest path using only 4-directional movement. Return the path length or -1 if unreachable.

Algorithms & Data Structures
Author's notes

Warm-up part, standard BFS.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem constraints (grid size, obstacles, start/exit representation) and then propose BFS as the optimal algorithm for unweighted shortest path. Walk through the BFS implementation, explaining how you track visited cells and distances, and analyze time and space complexity.

Pro tip: Mention that BFS is preferred over DFS because it guarantees the shortest path in unweighted graphs, and discuss potential optimizations like bidirectional BFS for large grids. Also, handle edge cases like start equals exit or no path exists.

1. Clarify the problem

Ask about grid dimensions, movement constraints, representation of start/exit, and whether obstacles are present. Confirm that the path length is the number of steps.

2. Choose the algorithm

Explain that BFS is ideal for finding the shortest path in an unweighted grid. Mention that DFS would not guarantee the shortest path.

3. Outline BFS approach

Describe using a queue to explore level by level, a visited set to avoid cycles, and tracking distance from start. Detail how to process neighbors in 4 directions.

4. Analyze complexity

State that time complexity is O(R*C) where R and C are grid dimensions, and space complexity is O(R*C) for the queue and visited set in the worst case.

5. Handle edge cases

Discuss cases like start equals exit (return 0), unreachable exit (return -1), and grids with no obstacles. Mention potential optimizations like bidirectional BFS.

Key Points to Mention

  • BFS guarantees shortest path in unweighted graphs
  • Use a queue for level-order traversal
  • Track visited cells to avoid infinite loops
  • Check all 4 directions (up, down, left, right)
  • Return -1 if the exit is unreachable
  • Time and space complexity: O(R*C)

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

Q2

Extend the maze problem: lowercase letters are keys, uppercase letters are doors. You can't pass through a door without its key. Find the shortest path from start to exit.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is where things got real.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the maze as a graph where each state is (position, set of keys collected). Use BFS to find the shortest path, since all moves have equal cost. When encountering a door, only proceed if the corresponding key is in the set; when encountering a key, add it to the set.

Pro tip: Mention that the state space can be reduced by only tracking keys that are actually needed for doors on the path, but be prepared to discuss the trade-off between memory and simplicity. Also, note that BFS guarantees the shortest path in terms of steps, but if there are weighted moves, Dijkstra's algorithm would be needed.

1. Clarify the problem

Ask clarifying questions: Can multiple keys of the same type exist? Are keys reusable? Can doors be opened without keys? What are the grid dimensions? This ensures you understand constraints.

2. Define state representation

Represent each state as (row, col, keys_bitmask). Use a bitmask to efficiently track which keys have been collected, assuming a limited number of key types (e.g., up to 26).

3. Choose BFS for shortest path

Since each move costs 1, BFS explores states in order of increasing path length, guaranteeing the shortest path. Use a queue and a visited set to avoid revisiting states.

4. Handle transitions

From a state, try all four directions. If the next cell is a wall, skip. If it's a door, check if the key is in the bitmask; if not, skip. If it's a key, update the bitmask. If it's the exit, return the distance.

5. Analyze complexity and trade-offs

Time complexity: O(R*C*2^K) where K is number of key types. Space: O(R*C*2^K). Discuss potential optimizations like pruning or A* if needed.

Key Points to Mention

  • State space explosion due to keys: exponential in number of key types.
  • Bitmask for efficient key set representation.
  • BFS guarantees shortest path for unweighted graphs.
  • Visited set must include key state to avoid cycles.
  • Handling of multiple keys of same type (if allowed).
  • Trade-offs: memory vs. time, and possible optimizations like bidirectional BFS or A* with admissible heuristic.

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

Q3

Now add an energy budget: each move costs 1 energy, some cells refill energy by a given amount, and you need to reach the exit with energy >= 0. How do you solve this?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I went straight to Dijkstra and the interviewer asked me to justify why not BFS.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as a shortest path on a state graph where each state is (cell, energy), and use Dijkstra's algorithm with energy as part of the cost. Since energy can be refilled, the state space is finite if we cap energy at the maximum needed, and we can optimize by tracking the maximum energy achievable at each cell for a given cost.

Pro tip: Mention that you can reduce the state space by observing that you never need more energy than the maximum refill amount plus the distance to the exit, and that you can use a modified Dijkstra where you maximize energy for the same cost.

1. Clarify constraints and assumptions

Ask about grid size, energy refill amounts, whether energy can exceed initial max, and if moves are 4-directional. Confirm that energy cannot go negative at any point.

2. Define state and graph

Define state as (row, col, energy). Each move to an adjacent cell costs 1 energy, and landing on a refill cell adds its amount. The goal is to reach the exit with energy >= 0.

3. Choose algorithm and handle cycles

Use Dijkstra's algorithm where the cost is the number of moves (or total energy spent) and we track the maximum energy achievable for each cell at a given cost. Alternatively, use BFS with a priority queue on energy to avoid cycles.

4. Optimize state space

Cap energy at a maximum value (e.g., max refill + grid size) to keep state space finite. Use a 2D array of best energy per cell to prune dominated states.

5. Analyze complexity and trade-offs

Time complexity O(R*C*E_max log(R*C*E_max)) where E_max is capped energy. Discuss trade-offs between BFS, Dijkstra, and A* if heuristic available.

Key Points to Mention

  • State space explosion and how to bound energy
  • Dijkstra's algorithm vs BFS with priority queue
  • Handling refill cells and ensuring energy never negative
  • Dominance pruning: for same cell and cost, keep max energy
  • Complexity analysis and potential optimizations
  • Edge cases: unreachable exit, initial energy insufficient, refill cells on path

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

Q4

Final extension: visit K target cells in any order, minimize total travel cost, then return to start. How do you approach this optimally?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I knew this was TSP territory the second they said 'any order' and 'minimize total cost.' Held-Karp DP after precomputing pairwise BFS distances between all targets.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Recognize this as a variant of the Traveling Salesman Problem where you must visit K of N target cells (not all) and return to start, so the optimal strategy depends on K relative to N. Discuss both exact approaches (e.g., DP over subsets for small K) and heuristic/approximation approaches (e.g., greedy nearest neighbor, MST-based) for large K, and analyze trade-offs in time and solution quality.

Pro tip: Clarify constraints first (grid size, K, obstacles, movement rules) and state that if K is small, exact DP is feasible, but if K is large, you should discuss approximation algorithms and their guarantees—showing you understand practical engineering trade-offs.

1. Clarify problem constraints

Ask about grid dimensions, number of targets N, K value, movement allowed (4-directional vs 8-directional), obstacles, and whether targets can be revisited. This determines the appropriate algorithmic approach.

2. Model as graph problem

Represent the grid as a graph where nodes are start and target cells, and edge weights are shortest path distances (e.g., BFS for unweighted grid). This reduces the problem to finding a minimum-cost cycle visiting exactly K target nodes.

3. Choose exact or approximate algorithm

If K is small (≤ ~15), use dynamic programming over subsets (Held-Karp style) to find optimal tour. If K is large, use approximation algorithms like nearest neighbor, Christofides, or MST-based heuristics, and discuss their time complexity and approximation ratios.

4. Analyze complexity and trade-offs

Compare exact DP (O(2^K * K^2)) vs heuristics (O(K^2) or O(K^3)). Discuss memory usage, scalability, and whether optimality is required or a good-enough solution suffices.

5. Optimize and handle edge cases

Consider precomputing all-pairs shortest paths, pruning, or using A* for large grids. Handle cases like K=0, K=N, unreachable targets, and multiple optimal solutions.

Key Points to Mention

  • Reduction to Traveling Salesman Problem (TSP) with a subset of nodes
  • Dynamic programming over subsets (Held-Karp) for exact solution when K is small
  • Approximation algorithms (nearest neighbor, Christofides, MST-based) for large K
  • Time and space complexity analysis (e.g., O(2^K * K^2) for DP)
  • Use of BFS/Dijkstra to compute shortest path distances between targets
  • Trade-offs between optimality, runtime, and implementation complexity

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

Q5

Walk through when you'd choose BFS vs Dijkstra vs A* for pathfinding problems, and how you'd structure the code so each sub-problem reuses components from the previous one.

System DesignTechnical Trade-offsAlgorithms & Data Structures
Author's notes

Honestly the most interesting part of the whole thing.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clearly defining the decision criteria for each algorithm based on graph properties (unweighted vs weighted, presence of heuristic) and performance trade-offs. Then, describe a modular code architecture where a common graph interface and traversal skeleton are reused, with algorithm-specific components plugged in. Emphasize how each algorithm builds upon the previous by generalizing the priority function and adding heuristics.

Pro tip: Mention that in practice, you'd often start with BFS for simplicity and only upgrade to Dijkstra or A* when performance requirements demand it, showing you balance engineering effort with optimization. Also, highlight that A* is essentially Dijkstra with a heuristic, so code reuse is natural.

1. Clarify problem characteristics

Ask about graph size, edge weights, whether a heuristic is available, and if optimality is required. This determines which algorithm is suitable.

2. Choose the right algorithm

Explain when to use BFS (unweighted, shortest path in terms of edges), Dijkstra (weighted, non-negative, no heuristic), and A* (weighted with admissible heuristic for faster goal-directed search).

3. Design reusable components

Propose a common graph representation (adjacency list) and a generic traversal function that takes a priority queue and a cost function. BFS uses a FIFO queue, Dijkstra uses a min-heap by distance, A* uses a min-heap by f-score.

4. Show incremental code evolution

Illustrate how BFS code can be refactored to Dijkstra by swapping the queue for a priority queue and adding distance tracking. Then, extend Dijkstra to A* by incorporating a heuristic in the priority calculation.

5. Discuss trade-offs and optimizations

Mention time/space complexity, early termination, and practical considerations like using a visited set, handling negative weights, and heuristic admissibility.

Key Points to Mention

  • BFS: O(V+E) time, optimal for unweighted graphs, uses queue.
  • Dijkstra: O((V+E) log V) with binary heap, requires non-negative weights, uses priority queue.
  • A*: O((V+E) log V) but often faster with good heuristic, requires admissible heuristic for optimality.
  • Code reuse: common graph interface, generic traversal with pluggable priority and cost functions.
  • Heuristic function: must be admissible (never overestimates) and consistent for efficiency.
  • Trade-offs: BFS simpler but limited; Dijkstra more general but slower; A* faster but needs heuristic.

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