Course Schedule II

Graphs Hard Problems Hard
  • This problem presents a real-world application of the topological sort algorithm, which is commonly used in project scheduling, ordering of compilation tasks, determining the sequence of tasks in project management, and resolving dependencies in package installation
  • In software development, it helps to identify the order of execution of interdependent classes or frameworks
  • An interesting use of this concept is in social media algorithms to determine the order of displaying posts based on relevance and dependency

There are a total of N tasks, labeled from 0 to N-1. Given an array arr where arr[i] = [a, b] indicates that you must take course b first if you want to take course a. Find the order of tasks you should pick to finish all tasks.

Examples:

Input: N = 4, arr = [[1,0],[2,1],[3,2]]

Output: [0, 1, 2, 3]

Explanation: First,finish task 0, as it has no prerequisites. Then,finish task 1, since it depends only on task 0. After that,finish task 2, since it depends only on task 1. Finally,finish task 3, since it depends only on task 2

Input: N = 4, arr = [[0,1],[3,2],[1,3],[3,0]]

Output: []

Explanation: It is impossible to finish all the tasks. Let’s analyze the pairs:

For pair {0, 1} → we need to finish task 1 first and then task 0 (order: 1 → 0).

For pair {3, 2} → we need to finish task 2 first and then task 3 (order: 2 → 3).

For pair {1, 3} → we need to finish task 3 first and then task 1 (order: 2 → 3 → 1 → 0).

But for pair {3, 0} → we need to finish task 0 first and then task 3, which contradicts the previous order. So, it is not possible to finish all the tasks.

Input: N = 2, arr = [[1,0]]

Constraints

  •   1 <= N <= 2000
  •   0 <= arr.length <= 5000
  •   arr[i].length == 2
  •   0 <= arr[i][0], arr[i][1] < N
  •   All the pairs arr[i] are unique.

Hints

  • "This method relies on in-degree tracking, which counts how many prerequisites a task has. Any task with an in-degree of 0 (i.e., no prerequisites) can be executed immediately."
  • As each task is completed, it reduces the in-degree of dependent tasks, allowing more tasks to become available for execution. If at any point, no tasks with 0 in-degree remain while tasks are still unprocessed, this means a cycle exists.

Company Tags

Zynga Epic Games Optum American Express Deloitte Rakuten McKinsey & Company Airbnb Activision Blizzard ARM Roblox AMD Salesforce Flipkart Instacart Cerner Seagate Technology Docker Western Digital Swiggy Nutanix Shopify Stripe Ernst & Young Teladoc Health Google Microsoft Amazon Meta Apple Netflix Adobe