← Meta Interview Insights

Meta·Machine Learning Engineer·Technical Phone Screen·Senior

Senior
May 2026

Summary

Meta ML engineer interview with a graph algorithm problem. Pretty standard coding round, nothing too wild, but DAG traversal always makes me second-guess my base cases.

Questions Asked (1)

Q1

Given a Directed Acyclic Graph (DAG), write a function that returns the length of the longest path in the graph.

Algorithms & Data Structures
Author's notes

My first instinct was BFS which was wrong.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem (e.g., path defined by number of edges or nodes, whether weights are involved) and then present a solution using topological sort with dynamic programming. Explain that you compute the longest path by processing nodes in topological order and updating distances to neighbors, ensuring O(V+E) time.

Pro tip: Mention that this approach works only for DAGs and that for general graphs, longest path is NP-hard. Also, note that you can optimize space by storing only the longest distance per node and updating a global maximum.

1. Clarify the problem

Ask whether the path length is measured in edges or nodes, and whether the graph is weighted. Confirm that the graph is a DAG and that we need the longest path from any node to any node.

2. Choose the algorithm

Select topological sort + dynamic programming. Explain that topological order ensures we process each node after all its predecessors, allowing us to compute the longest path ending at each node.

3. Outline the steps

Perform topological sort (e.g., Kahn's algorithm). Initialize a distance array with 0 for all nodes. Process nodes in topological order: for each neighbor, update its distance as max(current, distance[current] + 1) (or weight). Track the maximum distance seen.

4. Analyze complexity

State that topological sort takes O(V+E) time and O(V) space. The DP pass also takes O(V+E) time. Overall O(V+E) time and O(V) space.

5. Discuss edge cases and extensions

Handle empty graph, single node, disconnected components. Mention that if weights are negative, longest path is still well-defined in DAGs. For ML context, relate to DAGs in computation graphs or Bayesian networks.

Key Points to Mention

  • Topological sort is essential to process nodes in dependency order.
  • Dynamic programming: longest path to a node = max over predecessors (longest path to predecessor + edge weight).
  • Time and space complexity: O(V+E) time, O(V) space.
  • Works only for DAGs; for general graphs, longest path is NP-hard.
  • Can be implemented with DFS + memoization as an alternative.
  • In ML, DAGs appear in computation graphs (e.g., TensorFlow) and causal inference.

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