← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Junior

JuniorPrefer not to say
Jun 2026

Summary

Interviewed for a software engineer / ML intern role at Meta, two algorithmic questions back to back. Both were graph/grid problems which I should have been more prepared for.

Questions Asked (2)

Q1

Given an undirected, connected, acyclic graph (a tree) with n nodes and n-1 edges, find the length of the longest path between any two nodes. Describe your algorithm and analyze its time and space complexity.

Algorithms & Data Structures
Author's notes

This is the classic tree diameter problem.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify that the problem is about finding the diameter of a tree. Present the two-pass DFS/BFS algorithm: pick any node, find the farthest node A, then from A find the farthest node B; the distance between A and B is the diameter. Alternatively, describe a single DFS that computes the diameter by tracking the two longest downward paths at each node.

Pro tip: Mention that the two-pass BFS/DFS works because trees are acyclic and connected, and that the single DFS approach is more efficient for large trees as it avoids two traversals. Also, discuss edge cases like a single node (diameter 0) and skewed trees.

1. Clarify and Define

Confirm that the graph is a tree (connected, acyclic) and that the longest path is the tree's diameter. Define the problem clearly and ask about constraints (e.g., n up to 10^5).

2. Choose Approach

Decide between two-pass BFS/DFS or single DFS. Explain the two-pass method: from any node, find farthest node A; from A, find farthest node B; distance A-B is diameter. Or explain single DFS: for each node, compute the two longest downward paths and update the diameter.

3. Detail Algorithm

Walk through the chosen algorithm step-by-step. For two-pass: perform BFS/DFS to find farthest node, then repeat. For single DFS: recursively compute heights and track max sum of two longest child paths.

4. Analyze Complexity

State time complexity: O(n) for both approaches (each node visited once or twice). Space complexity: O(n) for recursion stack or queue, plus adjacency list storage.

5. Discuss Edge Cases and Trade-offs

Mention edge cases: n=1 (diameter 0), skewed tree (diameter n-1). Compare approaches: two-pass is simpler to implement, single DFS is more efficient for very large trees but requires careful recursion.

Key Points to Mention

  • Definition of tree diameter and its relation to longest path.
  • Two-pass BFS/DFS algorithm: pick arbitrary node, find farthest, repeat.
  • Single DFS approach: compute two longest downward paths at each node.
  • Time complexity O(n) and space complexity O(n) for both methods.
  • Proof of correctness: why the two-pass method works (farthest node from any node is an endpoint of a diameter).
  • Handling edge cases: single node, skewed tree, and recursion depth limits.

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

Q2

You have an n x n grid where 0 is a free cell and 1 is an obstacle. Starting from the top-left corner, find the shortest path to the bottom-right corner using BFS. Then, as a follow-up, describe how you would enumerate ALL simple paths and discuss the complexity and practical limits of doing so.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Part A was straightforward BFS, no surprises.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clearly explaining the BFS approach for the shortest path, including how to handle obstacles and track visited cells. Then, for the follow-up, describe a backtracking DFS to enumerate all simple paths, and analyze the exponential time complexity and practical limits such as grid size and path explosion.

Pro tip: Mention that BFS is optimal for unweighted grids, but for all paths, pruning techniques like early termination or memoization can help in some cases, though the problem remains exponential. Also, note that in practice, enumerating all paths is infeasible for large grids, so you'd use it only for small inputs or with additional constraints.

1. Clarify the problem and constraints

Confirm the grid size, movement directions (usually 4-directional), and that obstacles are impassable. Ask if diagonal moves are allowed or if there are any other constraints.

2. Explain BFS for shortest path

Describe using a queue to explore level by level, marking visited cells to avoid cycles, and tracking the distance or path. Mention that BFS guarantees the shortest path in an unweighted grid.

3. Discuss all simple paths enumeration

Explain using DFS with backtracking to explore all possible paths without revisiting cells. Emphasize that this is a brute-force approach and will be exponential in the worst case.

4. Analyze complexity and practical limits

For BFS, time and space are O(n^2). For all paths, time is exponential (e.g., O(4^(n^2)) in the worst case) and space is O(n^2) for recursion stack. Discuss that it's only feasible for very small grids (e.g., n <= 5 or 6).

5. Mention optimizations and alternatives

For all paths, suggest pruning (e.g., if only need count, use DP; if need paths, use backtracking with early exit). For shortest path, mention that BFS is optimal, but A* could be used if heuristic available.

Key Points to Mention

  • BFS uses a queue and explores level by level, ensuring shortest path in unweighted graphs.
  • Visited set is crucial to avoid infinite loops and redundant work.
  • All simple paths enumeration is typically done via DFS with backtracking, marking cells as visited and unmarking on backtrack.
  • Time complexity for all paths is exponential, often O(4^(n^2)) in the worst case, making it impractical for large n.
  • Space complexity for BFS is O(n^2) for the queue and visited set; for DFS, it's O(n^2) for recursion stack and visited set.
  • Practical limits: all paths enumeration is only feasible for very small grids (e.g., n <= 5 or 6) or with additional constraints like small number of paths.

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