This question evaluates proficiency in graph algorithms—particularly topological ordering and cycle detection—along with the use of efficient data structures for representing dependencies and analyzing time and space complexity.
Given an integer n representing tasks labeled 0..n-1 and a list of prerequisite pairs (a, b) meaning b must be completed before a, return any valid ordering of tasks that completes all tasks, or an empty list if no ordering exists. Design an efficient algorithm and data structures, explain how you detect cycles, and analyze time and space complexity. Follow-ups: (