Find a valid course order from prerequisites
Company: TikTok
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
You are given `numCourses` labeled from `0` to `numCourses-1` and a list of prerequisite pairs `prerequisites`, where each pair `[a, b]` means you must complete course `b` before you can take course `a`.
## Task
Return **one valid order** in which you can take all courses.
- If there are multiple valid answers, return any one of them.
- If it is **impossible** to finish all courses (because prerequisites contain a cycle), return an **empty list**.
## Input
- `numCourses`: integer
- `prerequisites`: list of pairs `[a, b]`
## Output
- A list of length `numCourses` representing a valid topological ordering, or `[]` if none exists.
## Constraints (reasonable interview defaults)
- `1 <= numCourses <= 2 * 10^5`
- `0 <= len(prerequisites) <= 2 * 10^5`
- `0 <= a, b < numCourses`
## Example
Input: `numCourses = 4`, `prerequisites = [[1,0],[2,0],[3,1],[3,2]]`
Output: `[0,1,2,3]` (or `[0,2,1,3]`)
Quick Answer: This question evaluates understanding of graph algorithms, topological ordering, dependency resolution, and cycle detection within the Coding & Algorithms domain, and represents a practical application of algorithmic concepts.