Course Schedule Cycle Detection
Updated
Problem statement
There are numCourses courses labeled from 0 to numCourses - 1. You are given a list prerequisites, where prerequisites[i] = [course, prerequisite] means that prerequisite must be completed before course.
Return true if it is possible to finish every course. Otherwise, return false.
Function
canFinish(numCourses: int, prerequisites: int[][]) → booleanExamples
Example 1
numCourses = 2prerequisites = [[1,0]]return = trueTake course 0 before course 1.
Example 2
numCourses = 2prerequisites = [[1,0],[0,1]]return = falseEach course depends on the other, so the dependency graph contains a directed cycle.
Example 3
numCourses = 4prerequisites = [[1,0],[2,1],[3,2]]return = trueThe courses can be completed in the order 0, 1, 2, 3.
Constraints
1 <= numCourses <= 2000.0 <= prerequisites.length <= 5000.prerequisites[i].length = 2.0 <= course, prerequisite < numCourses.- Every prerequisite pair is unique.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Course ScheduleAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Course ScheduleOracle · ONSITE INTERVIEWOracle interview questions
- Package Install OrderVerily · ONSITE INTERVIEW
- Task Build Order and File-Change RerunsVanta · PHONE SCREEN
- Course SchedulePlaud · ONSITE INTERVIEW
- All Halfway Courses Across Course TracksCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Audit Discounted Category AncestorsFaire · ONSITE INTERVIEW
- Course ScheduleNykaa · PHONE SCREENNykaa interview questions
- Detect a Cycle in a Directed GraphMakeMyTrip.com · ONSITE INTERVIEWMakeMyTrip.com interview questions
- Effective Role PrivilegesSnowflake · PHONE SCREENSnowflake interview questions