← Questions

Course schedule: detect a cycle and return an order

0
MetaSnowflakeCodingLast asked Sep 2025

A coding interview question asked in Meta's AI-enabled coding round and at Snowflake, last asked Sep 2025. It tests graphs, topological sort and dfs.

Courses have prerequisites. Given every course and what each one requires first, decide whether all of them can be taken, and if so return an order that respects every prerequisite. The same problem appears as installing packages that depend on other packages.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • course_order(courses: list[str], prereqs: dict[str, list[str]]) -> list[str] | None — prereqs[c] lists the courses that must come before c. Returns a valid order containing every course, or None if the prerequisites form a cycle.

Example

course_order(["db", "api", "web", "auth"],
             {"api": ["db", "auth"], "web": ["api"], "auth": ["db"]})
    ->  ["db", "auth", "api", "web"]          # any order with db before auth, both before api, api before web

course_order(["a", "b", "c"], {"a": ["b"], "b": ["c"], "c": ["a"]})
    ->  None                                  # a -> b -> c -> a

Constraints

  • Up to 10^5 courses and 10^5 prerequisite pairs.
  • The graph may be disconnected. A course may have no entry in prereqs.

Clarifying questions

  • Which direction do the edges point: "c needs p", or "p unlocks c"?
  • Is any valid order fine, or a specific one (for example, the lexicographically smallest)?
  • Can a prerequisite name a course that is not in the list?
  • When there is a cycle, should I return the cycle itself?

What a strong answer covers

  • DFS with three states (unvisited, in progress, done). Meeting an "in progress" node means a cycle. Appending each node when it is done gives the order.
  • Kahn's algorithm as the alternative: in-degree counts and a queue. If fewer than all nodes come out, there is a cycle.
  • Looping over every course so disconnected parts are covered.
  • Recursion depth: a chain of 10^5 courses overflows a recursive DFS in many languages. Write an iterative DFS or use Kahn's algorithm.
  • O(V + E) time and space. Tests: no prerequisites, a self-loop, two components, a long chain.

Follow-ups

  • Return the cycle when there is one.
  • Group the courses into semesters: the fewest terms if every course takes one term.
  • Install only what a given package needs, not everything.

Where it's asked

  • Meta · AI-enabled coding · Last asked Sep 202560 min · Several progressive tasks on a starter codebase in CoderPad with an AI assistant panel; usually two rounds · The first task usually without AI, later tasks with it
  • Snowflake · Two coding screens · Last asked Sep 202560 min · Two back-to-back live coding rounds, often on the same day; some loops pair coding with system design · Varies: some screens forbid AI, some 2026 screens allow it. The scheduling email says.

What it tests

graphs · topological sort · dfs

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding