Return a valid course completion order
Course Schedule Ordering
You are tasked with determining a valid sequence of courses to take, given a set of prerequisites.
You will be provided with:
- An integer
n, representing the total number of courses, labeled from0ton-1. - A list of prerequisite pairs,
prerequisites. Each pair[a, b]signifies that coursebmust be completed before courseacan be taken.
Your goal is to return any valid ordering of courses that allows all n courses to be completed. If it is impossible to complete all courses due to a cyclic dependency (e.g., course A requires B, and B requires A), you should return an empty list.
Requirements
- The solution must be efficient, capable of handling large numbers of courses (
n) and extensive prerequisite lists.
AI interview prep
Core dilemma
Efficiently finding a valid linear ordering of tasks with dependencies while robustly detecting and reporting unresolvable circular dependencies.
2 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Return a valid course completion order
Course Schedule Ordering
You are tasked with determining a valid sequence of courses to take, given a set of prerequisites.
You will be provided with:
- An integer
n, representing the total number of courses, labeled from0ton-1. - A list of prerequisite pairs,
prerequisites. Each pair[a, b]signifies that coursebmust be completed before courseacan be taken.
Your goal is to return any valid ordering of courses that allows all n courses to be completed. If it is impossible to complete all courses due to a cyclic dependency (e.g., course A requires B, and B requires A), you should return an empty list.
Requirements
- The solution must be efficient, capable of handling large numbers of courses (
n) and extensive prerequisite lists.