DIY: Course Schedule II

Solve the interview question "Course Schedule II" in this lesson.

Problem statement

There is a total of n courses labeled from 0 to n - 1. Some courses may have prerequisites. For example, if prerequisites[i] = [ai, bi], you must take course b before course a.

Given the total number of courses n and a list of the prerequisite pairs, return the course order one should take to finish all of the courses.

Input

Here is an example input:

n = 4 
prerequisites = [[1,0],[2,0],[3,1],[3,2]]

Output

Here is an example output:

[0,2,1,3]

Another valid ordering is [0,1,2,3].

Coding exercise

Implement the find_order(n, prerequisites) function, where n is the number of courses and prerequisites is the list of prerequisites. The function returns the order in which a student can take all of the courses.

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.