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 an array of the prerequisite pairs, return the course order one should take to finish all of the courses.


Here is an example input:

n = 4 
prerequisites = {{1,0},{2,0},{3,1},{3,2}}


Here is an example output:


Another valid ordering is {0,1,2,3}.

Coding exercise

Implement the findOrder(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 70+ hands-on prep courses.