Course Completion Order
Updated
Problem statement
There are numCourses courses numbered from 0 through numCourses - 1. Each pair [course, prerequisite] means the prerequisite must be completed before that course.
Return an order that completes every course exactly once, with each course after its prerequisites. If several courses are available at the same time, take the one with the smallest ID first.
Return an empty list if finishing every course is impossible because of a cycle. Zero courses also returns an empty list. Repeated prerequisite pairs do not change which orders are valid.
This is Course Schedule II with a small twist: the empty-list result and the smallest-ID rule above.
Function
courseOrder(numCourses: int, prerequisites: int[][]) → int[]Examples
Example 1
numCourses = 4prerequisites = [[2,0],[2,1],[3,2]]return = [0,1,2,3]Courses 0 and 1 are initially available; choose 0 then 1, which unlocks 2, then 3.
Example 2
numCourses = 3prerequisites = [[0,1],[1,0]]return = []Courses 0 and 1 each need the other, so neither can go first. Course 2 alone does not finish the plan, so the answer is an empty list.
Example 3
numCourses = 3prerequisites = []return = [0,1,2]No course has a prerequisite, so the smallest-ID rule gives the courses in increasing ID order.
Constraints
0 <= numCourses <= 200and0 <= prerequisites.length <= 1000.- Each prerequisite pair has two IDs in
[0, numCourses). WhennumCoursesis zero, the pair list is empty. - Cycles, self-dependencies, and repeated pairs are allowed.
More Indeed Phone Screen questions
- Insert into an Unrolled Linked ListPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Recipes You Can PreparePHONE SCREEN · Seen May 2025
- Rotting Orange PropagationPHONE SCREEN · Seen May 2025
- Courses Shared by Every Student PairPHONE SCREEN · Seen Aug 2022
- First and Last Access Time per UserPHONE SCREEN · Seen Jul 2022
- Most Accessed Resource in Five MinutesPHONE SCREEN · Seen Jul 2022
- Resource Transition GraphPHONE SCREEN · Seen Jul 2022
- Count Disappearing Connected TilesPHONE SCREEN · Seen Jul 2022
- First Dictionary Word from Note LettersPHONE SCREEN · Seen Jul 2022
- Word Path Moving Only Right or DownPHONE SCREEN · Seen Jul 2022
Similar questions at other companies
- Merge Event PreferencesAirbnb · PHONE SCREEN · ONSITE INTERVIEWAirbnb interview questions
- Network Layer Execution OrderNVIDIA · PHONE SCREENNVIDIA interview questions
- Course Schedule IIAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Task Scheduler with Prerequisites and Execution PathsBrex · PHONE SCREEN
- Dependency-Aware Task SchedulerRippling · PHONE SCREENRippling interview questions
- Lexicographically Smallest Topological OrderByteDance · PHONE SCREEN · ONSITE INTERVIEWByteDance interview questions
- Merge Subsequences Into a Consistent Total OrderGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Lexicographic Dependency OrderIllumio · PHONE SCREENIllumio interview questions
- Topological Sort with Secondary OrderingRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Order Sources by Their DependenciesRetool · PHONE SCREENRetool interview questions