Blind 75
45 / 75Free practice

Course Schedule

Medium · Graphs

Jump to code

There are n courses labeled 0 through n−1. Each pair [a, b] requires taking b before a. Decide whether all courses can be completed.

Examples

Example 1

numCourses = 2
prerequisites = [[1,0]]

Output: true

Example 2

numCourses = 2
prerequisites = [[1,0],[0,1]]

Output: false

Constraints

  • 1 <= numCourses <= 1000
  • 0 <= prerequisites.length <= 1000
  • prerequisites[i].length == 2
  • 0 <= a[i], b[i] < numCourses
  • All prerequisite pairs are unique.
Need a hint?

An ordering exists exactly when the dependency graph has no directed cycle.

Asked in real interviews

Candidates reported versions of Course Schedule from these company interviews. Some company questions are locked and open with Premium or XP.

Browse all real coding interview questions

Accepted submissions mark this problem complete. Progress stays in this browser.

Run checks 5 examples. Submit checks all 7 cases. A free account runs this problem; no XP needed.
Language
Case 1
Input
[
  2,
  [
    [
      1,
      0
    ]
  ]
]
Expected
true
Case 2
Input
[
  2,
  [
    [
      1,
      0
    ],
    [
      0,
      1
    ]
  ]
]
Expected
false
Case 3
Input
[
  1,
  []
]
Expected
true
Case 4
Input
[
  4,
  [
    [
      1,
      0
    ],
    [
      2,
      1
    ],
    [
      3,
      2
    ]
  ]
]
Expected
true
Case 5
Input
[
  3,
  [
    [
      0,
      1
    ],
    [
      1,
      2
    ],
    [
      2,
      0
    ]
  ]
]
Expected
false

Drafts are saved per language in this browser when storage is available.