Return a valid task order
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Take-home Project
You are given `n` tasks labeled from `0` to `n - 1` and a list of prerequisite pairs `prerequisites`, where each pair `[a, b]` means task `b` must be completed before task `a`.
Return any valid order in which all tasks can be completed. If it is impossible because the dependency graph contains a cycle, return an empty list.
Example:
- Input: `n = 4`, `prerequisites = [[1,0],[2,0],[3,1],[3,2]]`
- Output: `[0,1,2,3]` or `[0,2,1,3]`
Your solution should run efficiently for large inputs.
Quick Answer: This question evaluates understanding of graph algorithms and dependency resolution, testing the ability to produce a valid ordering of tasks and recognize when cyclic prerequisites make completion impossible.