Find a Valid Course Completion Order
Updated
Problem statement
There are numCourses courses labeled from 0 to numCourses - 1. Each pair [course, prerequisite] means that prerequisite must be completed before course.
Return the lexicographically smallest order that completes every course. At each step, choose the smallest numbered course whose prerequisites have all been completed. If a directed cycle makes it impossible to complete every course, return an empty array.
Function
findCourseOrder(numCourses: int, prerequisites: int[][]) → int[]Examples
Example 1
numCourses = 4prerequisites = [[1,0],[2,0],[3,1],[3,2]]return = [0,1,2,3]Course 0 unlocks courses 1 and 2. Choosing 1 first makes the complete order lexicographically smallest.
Example 2
numCourses = 5prerequisites = [[2,0],[2,1],[3,1]]return = [0,1,2,3,4]Courses 0, 1, and 4 initially have zero indegree. The smallest available course is always chosen.
Example 3
numCourses = 2prerequisites = [[1,0],[0,1]]return = []The two courses form a directed cycle, so no complete order exists.
Constraints
1 <= numCourses <= 2000.0 <= prerequisites.length <= 5000.prerequisites[i].length == 2.0 <= course, prerequisite < numCourses.- Every prerequisite pair is unique, and a course is never its own prerequisite.
More Google Onsite Interview questions
- 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
- Route Pattern MatcherPHONE SCREEN · ONSITE 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 Schedule IIAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Course Schedule IIWalmart · ONSITE INTERVIEWWalmart interview questions
- Task Order With PrerequisitesDoorDash · PHONE SCREEN · ONSITE INTERVIEWDoorDash interview questions
- Course Schedule IIBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Course Schedule IIChainalysis · OAChainalysis interview questions
- Course SchedulePlaud · ONSITE INTERVIEW
- Lexicographically Smallest Topological OrderByteDance · PHONE SCREEN · ONSITE INTERVIEWByteDance interview questions
- Topological Sort with Secondary OrderingRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Lexicographically Smallest Course OrderLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- ETL Pipeline SchedulerUnity · ONSITE INTERVIEW