Course Order, Lowest Number First
Updated
Problem statement
There are n courses numbered from 0 to n - 1. Each pair prerequisites[i] = [a, b] means course a must be taken before course b.
Return an order in which all n courses can be taken, one at a time, so that every course comes after all of its prerequisites. If several orders are possible, return the one that takes lower-numbered courses first: compared position by position, it has the smaller course number at the first position where two orders differ.
If no order is possible, return an empty list.
Function
findCourseOrder(n: int, prerequisites: int[][]) → int[]Examples
Example 1
n = 5prerequisites = [[3,1],[0,1],[4,2]]return = [0,3,1,4,2]Course 1 needs courses 3 and 0 first, and course 2 needs course 4 first. At the start, courses 0, 3 and 4 have no prerequisites. Take the smallest, 0. Then take 3, which makes course 1 ready (both of its prerequisites are done). Now 1 and 4 are ready, so take 1, then 4, which makes 2 ready, and finally 2. The order is [0, 3, 1, 4, 2]. Other orders such as [0, 3, 4, 1, 2] are also valid, but at the third position they have 4 where this one has the smaller 1.
Example 2
n = 3prerequisites = [[0,1],[1,2],[2,0]]return = []Course 0 must come before 1, 1 before 2, and 2 before 0. That is a cycle, so no order exists and the answer is an empty list.
Example 3
n = 3prerequisites = []return = [0,1,2]There are no prerequisites, so every order is valid. The one that takes lower-numbered courses first is [0, 1, 2].
Constraints
1 <= n <= 20000 <= prerequisites.length <= 10^4prerequisites[i].length == 20 <= prerequisites[i][0], prerequisites[i][1] < nprerequisites[i][0] != prerequisites[i][1]- All pairs in
prerequisitesare distinct.
More Nutanix Online Assessment questions
- Good SubsequencesOA · Seen Jul 2026
- Min Cost to Connect All PointsOA · Seen Jun 2026
- Digit Changes for a Multiple of 9OA · Seen Aug 2025
- Gas Station Loop StartOA · Seen Aug 2025
- Number of Players (for MTS2)OA · Seen Oct 2024
- Work Schedules (for MTS2)OA · Seen Oct 2024
- Minimum Operations to Convert ArrayOA · Seen Apr 2024
- No Adjacent CharactersOA · Seen Apr 2024
- Build a BST From a ListOA · Seen Nov 2022
- Delete Even-Valued Nodes From a Linked ListOA · Seen Nov 2022
Similar questions at other companies
- Course Schedule IIChainalysis · OAChainalysis interview questions
- Lexicographically Smallest Topological OrderIntuit · OAIntuit interview questions
- Task Scheduler with Prerequisites and Execution PathsBrex · PHONE SCREEN
- Course Schedule IIAmazon · PHONE SCREEN · ONSITE INTERVIEW
- 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
- Lexicographically Smallest Course OrderLinkedIn · ONSITE INTERVIEWLinkedIn interview questions