Course Schedule II

Asked atAtlassianFlipkartLinkedinPaytmSalesforceUber
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below

The problem

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. If no such ordering exists, return an empty array. Since multiple valid answers are possible, you can return any answer.

Note : The output visible will be 1 if your solution is correct, otherwise -1.

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]]

  • 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.

cpp

class Solution {
  public: vector < int > findOrder(int N, vector < vector < int >> arr) {
   
  }
};

java

class Solution {
    public int[] findOrder(int N, int[][] arr) {
       
    }
}

python

class Solution:
    def findOrder(self, N, arr):

javascript

class Solution {
    findOrder(N, arr) {
        
    }
}

csharp

class Solution {
    public int[] FindOrder(int N, int[][] arr) {

    }
}

go

func findOrder(N int, arr [][]int) []int {

}
Stuck? Show a way to structure it+
  1. 01Translate each prerequisite [course, prerequisite] into an edge prerequisite -> course.
  2. 02Compute every node's indegree and enqueue all zero-indegree courses.
  3. 03Repeatedly remove a course, append it to the order, and release its dependent courses.
  4. 04Return the order only if it contains all N courses; otherwise a cycle exists and the answer is empty.

Reference answer

Then expect these follow-ups

  • How would you implement the same cycle check with DFS colors?

    Tests: graph fundamentals

  • How would you return the lexicographically smallest valid order?

    Tests: data-structure choice

Hiring now

Salesforce has 1,420 open roles right now

Also hiring: Linkedin, Paytm

landed applies to roles like these for you, on each company’s own site, with answers written for the role.

Get 5x interviews10 free auto-applies. No card.
  • Business Development Representative Belgium Market (Dutch Speaking)

    Ireland - Dublin

  • Service Cloud Account Executive

    France - Paris

  • Summer Intern 2027 - Content Writer

    India - Bangalore

Or practice this question first

Next question