← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Apr 2026

Summary

Meta coding screen for a software engineer role. One graph problem, pretty standard if you've seen it before, but the kind of thing that trips you up if you haven't thought about cycle detection in a while.

Questions Asked (1)

Q1

Given a number of courses and a list of prerequisite pairs, determine whether it's possible to complete all courses without a circular dependency.

Algorithms & Data Structures
Author's notes

Classic graph problem.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the courses and prerequisites as a directed graph where an edge from course A to course B means A is a prerequisite for B. Then determine if the graph contains a cycle; if it does, it's impossible to complete all courses, otherwise it's possible. Use either depth-first search (DFS) with cycle detection or Kahn's algorithm for topological sorting.

Pro tip: Clarify edge direction upfront (prerequisite -> dependent) to avoid confusion, and mention that Kahn's algorithm naturally detects cycles by checking if the topological order includes all courses. Also, discuss handling disconnected graphs and potential follow-up questions like returning a valid course order.

1. Clarify the problem and edge cases

Confirm that prerequisites are given as pairs [a, b] meaning b must be taken before a. Ask about constraints: number of courses, possible duplicate edges, self-loops, and whether the graph is guaranteed to be connected.

2. Model as a directed graph

Represent courses as nodes and prerequisites as directed edges. Choose an adjacency list representation for efficiency, especially for sparse graphs.

3. Choose an algorithm to detect cycles

Select either DFS with recursion stack (or colors) or Kahn's algorithm (BFS-based topological sort). Explain why cycle detection is equivalent to checking if all courses can be completed.

4. Walk through the algorithm

Describe the steps: for DFS, mark nodes as unvisited, visiting, visited and detect back edges; for Kahn's, compute in-degrees, use a queue, and count processed nodes. Mention that if the count equals the number of courses, no cycle exists.

5. Analyze complexity and discuss trade-offs

State time complexity O(V+E) and space complexity O(V+E). Compare DFS vs Kahn's: DFS may be simpler to implement recursively but can hit recursion limits; Kahn's is iterative and naturally gives a topological order.

Key Points to Mention

  • Graph representation: adjacency list for efficiency
  • Cycle detection via DFS (recursion stack/colors) or Kahn's algorithm (in-degree and queue)
  • Time and space complexity: O(V+E) where V is number of courses and E is number of prerequisites
  • Handling disconnected graphs: run algorithm on all unvisited nodes
  • Edge cases: self-loops, duplicate edges, empty input
  • Follow-up: returning a valid course order (topological sort) if possible

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