Can All Courses Be Finished
Updated
Problem statement
There are numCourses courses, labeled from 0 to numCourses - 1. Each pair prerequisites[i] = [a, b] means you must finish course b before you can take course a.
You take one course at a time. Return true if there is an order in which you can finish every course, and false otherwise.
Function
canFinishAll(numCourses: int, prerequisites: int[][]) → booleanExamples
Example 1
numCourses = 4prerequisites = [[2,0],[1,2],[3,1]]return = trueCourse 2 needs course 0, course 1 needs course 2, and course 3 needs course 1. Take them in the order 0, 2, 1, 3, so the answer is true.
Example 2
numCourses = 3prerequisites = [[0,1],[1,2],[2,0]]return = falseCourse 0 needs 1, course 1 needs 2, and course 2 needs 0. Each of the three courses waits for another one in the loop, so none of them can be taken first. The answer is false.
Example 3
numCourses = 5prerequisites = [[3,1],[3,4],[1,0],[4,0],[2,3]]return = trueCourse 3 needs courses 1 and 4, both of which need course 0, and course 2 needs course 3. Course 3 can be reached from course 0 along two routes, but that is not a loop. The order 0, 1, 4, 3, 2 works, so the answer is true.
Constraints
1 <= numCourses <= 8000 <= prerequisites.length <= 3000prerequisites[i].length == 2- For each pair
[a, b]:0 <= a, b < numCoursesanda != b. - No pair appears more than once.
More C3.ai interview questions
- Every Result of Grouping an ExpressionONSITE INTERVIEW · Seen Jan 2023
- Fewest Jumps to the Last IndexONSITE INTERVIEW · Seen Jan 2023
- Rotate a Square Grid ClockwiseONSITE INTERVIEW · Seen Jan 2023
- Movie Schedule for Maximum EnjoymentONSITE INTERVIEW · Seen Jan 2023
- Days Until a Warmer DayPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2022
- Longest Arithmetic SubsequenceONSITE INTERVIEW · Seen Feb 2021
- Gas Station LoopONSITE INTERVIEW · Seen Dec 2020
- Drop Recent Duplicate MessagesOA · Seen Oct 2026
- Maximum K-Star SumOA · Seen Oct 2026
- Fewest Turns to Open a Wheel LockPHONE SCREEN · Seen Nov 2024
Similar questions at other companies
- Course SchedulePlaud · ONSITE INTERVIEW
- Course ScheduleAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Course ScheduleFlexTrade · ONSITE INTERVIEWFlexTrade interview questions
- Course ScheduleBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Trace Terminal Company OwnershipPalantir · ONSITE INTERVIEWPalantir interview questions
- Course ScheduleNykaa · PHONE SCREENNykaa interview questions
- Course Schedule FeasibilityTesla · PHONE SCREENTesla interview questions
- Can You Finish All Courses?Intuit · OAIntuit interview questions
- Course Schedule Cycle DetectionGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Island Count and Maximum AreaOracle · ONSITE INTERVIEWOracle interview questions