Reported September 2026
Amazongraph

Course Schedule II

Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Amazon OA. Under 2s to a working solution.
Founder's read

The detail that matters in this Amazon OA question from September 2026 is one line: return the lexicographically smallest ordering. That turns plain Course Schedule II into a topological sort with a twist. If you've seen the standard version, you already know 80% of it. The other 20% is what separates a pass from a wrong-answer wall on the hidden tests. Graph with up to 2000 nodes and 5000 edges, cycle means return an empty array. If you blank on the tie-breaking part during the live assessment, StealthCoder runs invisibly on your screen and gives you the solution as a safety net.

The problem

There are numCourses courses labeled from 0 to numCourses - 1. Each pair [course, prerequisite] means the prerequisite must be completed before the course.
Return the lexicographically smallest course ordering that satisfies every prerequisite. If no valid ordering exists, return an empty array.

Function
findCourseOrder(numCourses: int, prerequisites: int[][]) → int[]

Examples
Example 1
numCourses = 2
prerequisites = [[1,0]]
return = [0,1]
Course 0 must appear before course 1.
Example 2
numCourses = 4
prerequisites = [[1,0],[2,0],[3,1],[3,2]]
return = [0,1,2,3]
After course 0, courses 1 and 2 are both available, so lexicographic order chooses 1.
Example 3
numCourses = 2
prerequisites = [[1,0],[0,1]]
return = []
The two courses form a cycle, so no complete ordering 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.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The pattern is Kahn's algorithm with a min-heap instead of a plain queue. Build an adjacency list from each [course, prerequisite] pair and count indegrees. Push every course with indegree 0 into a min-heap. Pop the smallest, append it to the result, then decrement indegree for each neighbor and push any that hit 0. The heap guarantees you always take the smallest available course, which is exactly what Example 2 shows when it picks 1 before 2. The common pitfall is using a regular queue, which gives a valid order but not the smallest one. Second pitfall: forgetting the cycle check. If the result length is less than numCourses at the end, return an empty array. Complexity is O((V + E) log V), which is fine for these limits. StealthCoder is the hedge if the heap tie-break slips your mind mid-assessment.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Course Schedule II cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as course schedule ii. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Amazon's OA.

Amazon reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Course Schedule II FAQ

What's the trick in Course Schedule II with lexicographic order?+

Swap the queue in Kahn's algorithm for a min-heap. Every time you pick the next course, you take the smallest index among all courses with zero remaining prerequisites. A normal queue gives a valid order, but not necessarily the smallest one, so hidden tests will fail.

How do I detect the cycle case?+

Count how many courses you've output. If the final result has fewer than numCourses entries, some courses never reached indegree 0, which means a cycle exists. Return an empty array. No separate DFS coloring is needed when you use Kahn's approach.

Is DFS a good option here?+

Not really. DFS postorder gives a valid topological order, but forcing the lexicographically smallest result is awkward with it. Kahn's with a min-heap is the cleaner path and easier to get right under time pressure in an Amazon OA.

What's the time complexity and will it pass the limits?+

O((V + E) log V) with a heap. With 2000 courses and 5000 prerequisites, that's tiny. You won't hit performance problems. The only way to fail is a logic bug in the tie-breaking or cycle check.

How do I prepare for this in 48 hours?+

Write Kahn's algorithm from memory twice, once with a queue and once with a heap. Test on the three given examples, plus a case with no prerequisites and a case with a cycle. That covers every branch this question can throw at you.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Amazon.

OA at Amazon?
Invisible during screen share
Get it