Problem · Graph

Course Schedule

Learn this problem
MediumAmazon logoAmazonONSITE INTERVIEW
See Amazon hiring insights

Problem statement

There are numCourses courses labeled from 0 to numCourses - 1. Each pair [course, prerequisite] means the prerequisite must be completed before the course.

Return true if all courses can be completed, or false if the prerequisite graph contains a cycle.

Interview Follow-up

The interviewer asked follow-up questions about alternative solutions, edge cases, and complexity analysis.

Function

canFinish(numCourses: int, prerequisites: int[][]) → boolean

Examples

Example 1

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

Example 2

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

Each course requires the other first, creating a cycle.

More Amazon problems

drafts saved locally
public boolean canFinish(int numCourses, int[][] prerequisites) {
  // write your code here
}
numCourses2
prerequisites[[1,0]]
expectedtrue
checking account