Course Schedule
Updated
Problem statement
Given numCourses courses and a list of prerequisites, return true if you can finish every course, or false if you cannot.
The courses are 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 may take the courses in any order that keeps every pair.
Function
canFinish(numCourses: int, prerequisites: int[][]) → booleanExamples
Example 1
numCourses = 5prerequisites = [[2,0],[2,1],[4,2],[3,1]]return = trueCourses 0 and 1 need nothing. Take 0, then 1, then 2 (it needs 0 and 1), then 3 (it needs 1), then 4 (it needs 2). Every course gets finished.
Example 2
numCourses = 4prerequisites = [[1,0],[2,1],[3,2],[1,3]]return = falseCourse 1 needs course 3, course 3 needs course 2, and course 2 needs course 1. These three courses wait on each other, so none of them can ever be taken. Only course 0 can be finished.
Example 3
numCourses = 3prerequisites = [[1,0],[2,2]]return = falseThe pair [2, 2] says course 2 must be finished before course 2 itself, which is impossible. Courses 0 and 1 can be taken, but course 2 never can.
Constraints
1 <= numCourses <= 20000 <= prerequisites.length <= 5000prerequisites[i].length == 20 <= prerequisites[i][j] < numCourses- All pairs in
prerequisitesare distinct.
More Oracle Onsite Interview questions
- Shortest Subarray with Sum at Least KONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Minimum Meeting RoomsONSITE INTERVIEW · Seen Nov 2026
- Shortest Window with Required CountsONSITE INTERVIEW · Seen Nov 2026
- Maximum Subarray Sum with One DeletionONSITE INTERVIEW · Seen Nov 2026
- Shortest Deletion for Distinct CharactersONSITE INTERVIEW · Seen Nov 2026
- Count Dropped Rate-Limited RequestsONSITE INTERVIEW · Seen Nov 2026
- Design Authentication ManagerONSITE INTERVIEW · Seen Oct 2026
- Maximum Bouquet ValueONSITE INTERVIEW · Seen Oct 2026
- Cache an Expensive Service Across ThreadsONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Course ScheduleAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Course SchedulePlaud · ONSITE INTERVIEW
- Course ScheduleFlexTrade · ONSITE INTERVIEWFlexTrade interview questions
- Course ScheduleBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Can All Courses Be FinishedC3.ai · ONSITE INTERVIEWC3.ai 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
- Package Install OrderVerily · ONSITE INTERVIEW