← Google Interview Insights

Google·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Google SWE interview with a meaty graph validation problem that covered both correctness checking and cycle detection. More algorithmic depth than I expected for a single question.

Questions Asked (1)

Q1

Design a function that validates an e-learning course catalog. Given a set of course IDs and a list of prerequisite pairs (u, v) meaning v requires u, determine whether the catalog is valid: no prerequisite references a course that doesn't exist, and there are no cycles in the dependency graph. If valid, return true and a valid course ordering. If not, return false along with any missing course references and at least one detected cycle. Describe your algorithm and its time and space complexity.

Algorithms & Data StructuresSystem DesignTechnical Trade-offs
Author's notes

This is basically two problems stapled together and I didn't see that fast enough.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the prerequisites as a directed graph and use topological sorting (Kahn's algorithm) to detect cycles and produce a valid ordering. First validate that all referenced courses exist, collecting any missing ones. Then attempt topological sort; if it fails, extract a cycle from the remaining nodes.

Pro tip: Explicitly discuss how you would extract a cycle when a cycle is detected, and mention that Kahn's algorithm naturally leaves the cyclic nodes in the queue, making it easy to identify a cycle. Also, clarify that the ordering is not unique and any valid topological order is acceptable.

1. Clarify requirements and edge cases

Confirm that course IDs are unique, prerequisites are directed edges, and that missing courses should be reported. Discuss handling of empty inputs, self-loops, and duplicate edges.

2. Validate course existence

Iterate through all prerequisite pairs and check if both u and v exist in the course set. Collect any missing course IDs into a list to return if invalid.

3. Build graph and compute in-degrees

Construct an adjacency list for the directed graph and compute the in-degree for each course. This prepares for topological sorting.

4. Topological sort with cycle detection

Use Kahn's algorithm: enqueue nodes with in-degree 0, repeatedly dequeue and reduce in-degrees of neighbors. If the sorted list contains all courses, return true and the ordering; otherwise, a cycle exists.

5. Extract and report a cycle

If a cycle is detected, perform a DFS on the remaining nodes (those with in-degree > 0) to find and return one cycle. Combine missing courses and cycle in the invalid response.

Key Points to Mention

  • Graph representation: adjacency list for prerequisites, with courses as nodes and edges u -> v.
  • Topological sorting using Kahn's algorithm (BFS-based) for O(V+E) time complexity.
  • Cycle detection: if topological sort fails to include all nodes, a cycle exists.
  • Cycle extraction: use DFS with recursion stack to find a cycle among nodes with remaining in-degree.
  • Missing course validation: check all referenced IDs against the course set.
  • Time and space complexity: O(V+E) time and O(V+E) space for graph storage and auxiliary data structures.

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