Course Schedule II: Return a Course Order
Updated
Problem statement
There are numCourses courses labeled from 0 through numCourses - 1. Each prerequisite pair [course, prerequisite] means the prerequisite must appear before the course in your schedule.
Return an ordering that includes every course exactly once and satisfies every prerequisite. If no such ordering exists, return an empty array.
For this exercise, assume that when several valid orders exist, you must return the lexicographically smallest one. Compare two orders at their first differing position; the order with the smaller course number there comes first.
Function
findOrder(numCourses: int, prerequisites: int[][]) → int[]Examples
Example 1
numCourses = 2prerequisites = [[1,0]]return = [0,1]Take course 0 before course 1.
Example 2
numCourses = 4prerequisites = [[1,0],[2,0],[3,1],[3,2]]return = [0,1,2,3]Both 1 and 2 become available after 0. Choose 1 first to obtain [0,1,2,3].
Example 3
numCourses = 1prerequisites = []return = [0]The only course has no prerequisites.
Example 4
numCourses = 2prerequisites = [[1,0],[0,1]]return = []Each course requires the other first, so no complete ordering exists.
Constraints
1 <= numCourses <= 20000 <= prerequisites.length <= numCourses * (numCourses - 1)- Every prerequisite pair has two distinct course labels between
0andnumCourses - 1. - Prerequisite pairs are distinct. Directed cycles may be present.
More Meta Onsite Interview questions
- Mutual-Friend RecommendationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Deterministic K-Means AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Linear Layer Forward and Backward PassesONSITE INTERVIEW · Seen Sep 2026
- Reservoir Sampling with Recorded DrawsONSITE INTERVIEW · Seen Sep 2026
- Stochastic Gradient Descent with MomentumONSITE INTERVIEW · Seen Sep 2026
- Top K Largest Values with a HeapONSITE INTERVIEW · Seen Sep 2026
- Multi-Head Attention Forward PassPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Partition a Card Deck into FifteensONSITE INTERVIEW · Seen Sep 2026
- Insert into a Sorted Circular ListONSITE INTERVIEW · Seen Aug 2026
- Maximum Stock Profit with Transaction LimitsONSITE INTERVIEW · Seen Aug 2026
Similar questions at other companies
- Course Schedule IIAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Lexicographically Smallest Topological OrderByteDance · PHONE SCREEN · ONSITE INTERVIEWByteDance interview questions
- Merge Subsequences Into a Consistent Total OrderGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle 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
- Microservice Deployment OrderGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- 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
Meta interview experiences
- Meta PhD SWE Intern AI Coding Phone Screen: 1 Experience
- Meta Business Support Engineer Phone Screen: 1 Experience
- Meta Software Engineer Onsite Questions: 3 Experiences
- Meta AI Native Engineer OA and Onsite: 1 Experience
- Meta AI-Native Coding Interview: 3 Experiences
- Meta Production Engineer Intern Questions: 6 Experiences