PracHub
QuestionsLearningGuidesInterview Prep

Quick Overview

Build a deterministic prerequisite ordering with recursive depth-first search. Handle disconnected classes and detect dependency cycles by tracking both active and completed nodes.

  • medium
  • Vanta
  • Coding & Algorithms
  • Software Engineer

Order Classes by Prerequisites with Recursive DFS

Company: Vanta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

# Order Classes by Prerequisites with Recursive DFS Implement `class_order(classes, prerequisites)` using recursive depth-first search. `classes` contains distinct class names. Each item `[course, prerequisite]` in `prerequisites` means `prerequisite` must be completed before `course`. Return an ordering containing every class exactly once and satisfying every prerequisite. The result must be deterministic: start DFS roots in the order given by `classes`, visit each course's prerequisites in the order their pairs appear in `prerequisites`, and append a course after all of its prerequisites. If the dependency graph contains a cycle, return `[]`. ## Function Contract ```python def class_order(classes: list[str], prerequisites: list[list[str]]) -> list[str]: ... ``` ## Examples ```text classes = ["Compilers", "Algorithms", "AI", "Databases"] prerequisites = [ ["Compilers", "Algorithms"], ["AI", "Algorithms"], ] result = ["Algorithms", "Compilers", "AI", "Databases"] ``` ```text classes = ["A", "B", "C"] prerequisites = [["A", "B"], ["B", "C"], ["C", "A"]] result = [] ``` ## Constraints - `0 <= len(classes) <= 1_000` - `0 <= len(prerequisites) <= 20_000` - Every prerequisite pair contains two names from `classes`. - There are no duplicate prerequisite pairs. - The implementation must use recursive DFS, with enough states to distinguish an active recursion path from a completed node.

Quick Answer: Build a deterministic prerequisite ordering with recursive depth-first search. Handle disconnected classes and detect dependency cycles by tracking both active and completed nodes.

Implement class_order(classes, prerequisites) using recursive depth-first search. classes contains distinct class names. Each pair [course, prerequisite] means the prerequisite must be completed before the course. Return every class exactly once in a valid order. For deterministic output, start DFS roots in classes order, visit each course's prerequisites in pair-input order, and append a course after all its prerequisites. Return [] if a cycle exists.

Constraints

  • 0 <= len(classes) <= 1,000
  • 0 <= len(prerequisites) <= 20,000
  • Class names are distinct strings and every pair references listed classes.
  • There are no duplicate prerequisite pairs.
  • The implementation uses recursive DFS with active and completed states.

Examples

Input: ([], [])

Expected Output: []

Explanation: The empty catalog has an empty valid order.

Input: (["A"], [])

Expected Output: ["A"]

Explanation: A lone class has no prerequisites.

Hints

  1. Build each course's prerequisite list without reordering the pairs.
  2. Three states—unvisited, active, and completed—distinguish a back edge from shared work.
  3. Append a course only after every prerequisite DFS returns successfully.
Last updated: Jul 18, 2026

Loading coding console...

PracHub

Master your tech interviews with 9,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement Adjacent-Line Uniq - Vanta (medium)
  • Implement a Unique Lines Command - Vanta (easy)
  • Remove Global Duplicates While Preserving Order - Vanta (medium)
  • Implement a uniq-like function - Vanta (medium)