Reported September 2026
Amazongraph

Course Order and Cycle

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

Numbers up to 10^5 courses and 2 * 10^5 prerequisites kill any brute-force idea before you write a line. This is the Amazon "Course Order and Cycle" OA, reported in September 2026, and it's a topological sort with two twists: the order must be lexicographically smallest, and if there's a cycle you have to return the exact one. You need linear-ish time and careful tie-breaking. If you blank on the cycle extraction mid-assessment, StealthCoder runs invisibly on your desktop and hands you a working solution as a safety net.

The problem

There are numCourses courses numbered from 0 through numCourses - 1. Each row [course, prerequisite] means the prerequisite must be completed before the course.
Return a two-row result [order, cycle]:
If all courses can be completed, return the lexicographically smallest valid topological order in order and an empty cycle.
If completion is impossible, return an empty order and one directed cycle in cycle. Do not repeat the first course at the end.
To make cycle selection deterministic, inspect starting courses in increasing order and each course's outgoing neighbors in increasing order. Return the active-path segment closed by the first back edge encountered.

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

Examples
Example 1
numCourses = 4
prerequisites = [[1,0],[2,0],[3,1],[3,2]]
return = [[0,1,2,3],[]]
Course 0 is first. Courses 1 and 2 then become available, and the smaller course is chosen first.
Example 2
numCourses = 3
prerequisites = [[1,0],[2,1],[0,2]]
return = [[],[0,1,2]]
The directed edges form 0 -> 1 -> 2 -> 0, so no topological order exists.

Constraints
1 <= numCourses <= 10^5.
0 <= prerequisites.length <= 2 * 10^5.
Every prerequisite row contains two distinct valid course numbers.
No prerequisite edge appears more than once.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Two passes, one graph. First, build adjacency lists from [course, prerequisite], meaning the edge goes prerequisite to course. Run Kahn's algorithm with a min-heap instead of a plain queue. That gives the lexicographically smallest order in O((V+E) log V). If the output has numCourses entries, return it with an empty cycle. Otherwise run a DFS for the cycle. Start from courses in increasing order, sort each neighbor list ascending, and track a color state (unvisited, on path, done). The first time you hit a node that's on the current path, slice the path from that node to the end. That's your cycle, with no repeated first course. Pitfalls: recursion depth at 10^5 will overflow in many languages, so go iterative. Also don't return a cycle from Kahn's leftovers, since those nodes may just be downstream of a cycle. Follow the deterministic rules exactly.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Course Order and Cycle 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 passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ 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 passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Course Order and Cycle FAQ

What's the trick in the Amazon Course Order and Cycle problem?+

Use Kahn's algorithm with a min-heap so each step picks the smallest available course. That gives the lexicographically smallest topological order. If not every course gets output, switch to a DFS with path tracking to pull out the specific cycle.

How do I find the exact cycle the problem wants?+

Run DFS from courses in increasing order, visiting neighbors in increasing order. Keep a path stack and a state array. When you reach a node already on the active path, that's the first back edge. Return the stack segment from that node to the top.

Will a plain recursive DFS pass the constraints?+

Risky. With up to 10^5 courses, a long chain can blow the call stack in Python or Java. Write the DFS iteratively with an explicit stack and a per-node neighbor index so you can still detect back edges and rebuild the path.

Why can't I just use the leftover nodes from Kahn's as the cycle?+

Leftover nodes include everything stuck behind a cycle, not only the cycle itself. A node downstream of a cycle never reaches indegree zero but isn't part of it. You need DFS back-edge detection to isolate the real cycle.

How do I prepare for this in 48 hours?+

Write Kahn's with a heap and an iterative three-color DFS from scratch, twice. Test on a diamond graph, a simple 3-cycle, and a cycle with a tail leading into it. Check that the output has no repeated first node. That covers every branch of this problem.

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