← Questions

Validate a course catalog's prerequisites

0
GoogleCodingLast asked Aug 2025

A coding interview question asked in Google's first round (coding), last asked Aug 2025. It tests graphs and topological sort.

You are validating an online course catalog. You get the list of course ids and a list of prerequisite pairs (u, v), meaning course v requires course u (take u first). The catalog is valid when every course named in a pair exists, and the prerequisites contain no cycle. Report validity, an order to take every course, the missing references and one cycle.

Interface

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

  • validate(course_ids: list[str], prereqs: list[tuple[str, str]]) -> tuple[bool, list[str], list[str], list[str]] returning (valid, order, missing, cycle):
    • valid is true only with no missing references and no cycle;
    • order is a topological order of the existing courses, taking the smallest available id first; [] if there is a cycle;
    • missing is the sorted list of ids named in pairs but not in the catalog;
    • cycle is one cycle with its first course repeated at the end, such as ["A", "B", "C", "A"], or [].

Example

validate(["A", "B", "C"], [("A", "B"), ("B", "C")])
  ->  (True, ["A", "B", "C"], [], [])

validate(["A", "B", "C"], [("A", "B"), ("B", "C"), ("C", "A")])
  ->  (False, [], [], ["A", "B", "C", "A"])

validate(["db", "ml", "py", "web"], [("py", "ml"), ("db", "web"), ("py", "web"), ("stats", "ml")])
  ->  (False, ["db", "py", "ml", "web"], ["stats"], [])     # acyclic, but "stats" doesn't exist

Constraints

  • Up to 10^5 courses and 2 · 10^5 pairs.
  • Pairs that name a missing course are reported and left out of the graph.

Clarifying questions

  • Which direction does a pair point: u before v, or the other way?
  • Should an order still be returned when references are missing?
  • Any cycle, or all of them? Does the starting point of the reported cycle matter?
  • Can a course list itself as a prerequisite, or a pair appear twice?

What a strong answer covers

  • One pass over the pairs to collect missing ids, then a graph over existing courses only.
  • Kahn's algorithm with a min-heap of ready courses for the deterministic order; if fewer courses come out than exist, there is a cycle. O((V + E) log V), or O(V + E) without the tie-break.
  • Finding one concrete cycle: DFS with three colours over the nodes Kahn's algorithm couldn't place; a back edge to a grey node closes the cycle, sliced out of the current path.
  • Self-loops and duplicate pairs.
  • Tests: empty catalog, a self-loop, two independent cycles, missing ids on both sides of a pair.

Follow-ups

  • Return the minimum number of terms needed if any number of courses can be taken per term (longest path).
  • Add a limit of k courses per term.
  • The catalog changes often; re-validate incrementally after each new pair.

Where it's asked

  • Google · First round: coding · Last asked Aug 202545 min · Coding in a Google Doc, no syntax highlighting or autocomplete · No AI

What it tests

graphs · topological sort

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