PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates graph algorithm skills—specifically topological ordering, directed cycle detection, and analysis of time and space complexity. Common in Coding & Algorithms interviews, it examines reasoning about dependency resolution in directed graphs and requires practical algorithm implementation with emphasis on algorithmic complexity, placing it at the practical application and algorithmic-analysis level rather than purely conceptual understanding.

  • hard
  • Amazon
  • Coding & Algorithms
  • Software Engineer

Find a Valid Course Order

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Onsite

You are given n courses labeled from 0 to n - 1 and a list of prerequisite pairs. Each pair [course, prerequisite] means prerequisite must be completed before course. Return any valid ordering of all courses. If no ordering is possible because the prerequisites contain a cycle, return an empty list. Implement the solution using breadth-first topological sorting and analyze the time and space complexity.

Quick Answer: This question evaluates graph algorithm skills—specifically topological ordering, directed cycle detection, and analysis of time and space complexity. Common in Coding & Algorithms interviews, it examines reasoning about dependency resolution in directed graphs and requires practical algorithm implementation with emphasis on algorithmic complexity, placing it at the practical application and algorithmic-analysis level rather than purely conceptual understanding.

You are given an integer n representing courses labeled from 0 to n - 1 and a list of prerequisite pairs. Each pair [course, prerequisite] means the prerequisite course must be completed before the course. Return a valid ordering of all courses such that every prerequisite appears before the course that depends on it. If no such ordering exists because the prerequisite graph contains a cycle, return an empty list. Implement the solution using breadth-first topological sorting (Kahn's algorithm).

Constraints

  • 1 <= numCourses <= 100000
  • 0 <= len(prerequisites) <= 200000
  • Each prerequisite pair has the form [course, prerequisite]
  • 0 <= course, prerequisite < numCourses

Examples

Input: (4, [[1, 0], [2, 1], [3, 2]])

Expected Output: [0, 1, 2, 3]

Explanation: This is a simple chain: 0 must come before 1, 1 before 2, and 2 before 3.

Input: (2, [[1, 0]])

Expected Output: [0, 1]

Explanation: Course 0 must be taken before course 1.

Input: (3, [[1, 0], [2, 1], [0, 2]])

Expected Output: []

Explanation: The prerequisites form a cycle: 0 -> 1 -> 2 -> 0, so no valid order exists.

Input: (1, [])

Expected Output: [0]

Explanation: With only one course and no prerequisites, the only valid order is [0].

Input: (5, [[1, 0], [2, 1], [3, 1], [3, 2], [4, 3]])

Expected Output: [0, 1, 2, 3, 4]

Explanation: The dependency structure forces the order: 0 before 1, 1 before 2 and 3, 2 before 3, and 3 before 4.

Hints

  1. Treat courses as nodes in a directed graph where prerequisite -> course.
  2. Count how many prerequisites each course still needs, then start from all courses with zero remaining prerequisites.
Last updated: May 30, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

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

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL 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 Datacenter Router Commands - Amazon (hard)
  • Replace Delimited Tokens in a String - Amazon (medium)
  • Minimize Circular Redistribution Cost - Amazon (medium)
  • Find the Most Common Visit Pattern - Amazon (hard)
  • Maximize Value Under a Budget - Amazon (medium)