Sale
Summer Offer: $190/yr $99/yr
00 : 00 : 00 : 00
Get this deal
Coding Round 2 Mid-Level programming

LeetCode #210 - Course Schedule II

There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prerequisites, for example, to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]. Given the total number of courses and a list of prerequisite pairs, return the ordering of courses you should take to finish all courses. If it is impossible to finish all courses, return an empty array.

Problem Statement


def findOrder(numCourses: int, prerequisites: List[List[int]]) -> List[int]:

Example 1


Input: numCourses = 4, prerequisites = [[2,0],[1,0],[3,1],[3,2]]
Output: [0,1,2,3]
Explanation: There are multiple correct orders, but one possible order is [0, 1, 2, 3].

Example 2


Input: numCourses = 2, prerequisites = [[1,0],[0,1]]
Output: []
Explanation: There's no way to complete the courses because they depend on each other.

Constraints


  • The input prerequisites is a graph, where the number of nodes is numCourses and the number of edges is the length of prerequisites.
Suggested Answer

Trusted by 100+ professionals preparing for interviews

Trusted by 100+ professionals 50+ Company Question Banks 5+ Supported Languages

Practice More Questions Like This

Generate unlimited interview questions with structured answers, code runner, and AI-powered walkthroughs.

Get Started Free

More Coding Round 2 Interview Prep

LeetCode #200 - Number of Islands
Coding Round 1 · Mid-Level
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Link copied to clipboard