AO
Back

Course Schedule and Dependency Ordering (Topological Sort)

Also reported as Course Schedule II · LeetCode 207

CodingOnsiteLast reported September 2026High Frequency

Problem Overview

The round: 16 reports between October 2025 and September 2026, from intern, new-grad, SDE II, L5, MLE and senior Applied Scientist loops, in phone screens and onsite coding rounds. Most are LeetCode 207 and 210 (Course Schedule I and II), either as written or with an Amazon story on top: order tasks that have prerequisites, build a package after its dependencies, find the services hit when some go down. Several reports describe rounds that open with leadership-principle questions and leave the rest of the hour for code. One intern candidate notes that Amazon interviewers do not run the code, so explain the approach before writing it.

The page has four graded parts. Parts 2 to 4 are follow-ups reports describe after the base question.

Input (every part): prerequisites is a list of pairs [a, b] meaning b must be taken before a (LeetCode's convention). Courses (or tasks) are numbered 0 to n - 1. The tests use no repeated pairs and no course that is its own prerequisite.

Part 1 — can every course be finished?

def can_finish(num_courses: int, prerequisites: list) -> bool: ...
  • Return True if some order takes every course after all of its prerequisites, False if a cycle makes that impossible (LeetCode 207).

Part 2 — return a valid order

def find_order(num_courses: int, prerequisites: list) -> list: ...
  • Return every course exactly once, each after all of its prerequisites. Any valid order is accepted. If there is a cycle, return an empty list [] (LeetCode 210).

Part 3 — print the courses on a cycle

def find_cycle(num_courses: int, prerequisites: list) -> list: ...
  • If the prerequisites contain a cycle, return the courses on one cycle, in cycle order: each course in the list is a prerequisite of the next, and the last is a prerequisite of the first. The tests accept any one cycle, in either direction, with or without the first course repeated at the end. Return [] when there is no cycle.

Part 4 — which tasks must rerun?

def tasks_to_rerun(num_tasks: int, prerequisites: list, changed: list) -> list: ...
  • Some tasks in changed have changed. Return every task that must rerun: the changed tasks plus every task that depends on one of them, directly or through other tasks. Order them so each task comes after the tasks it needs from that set. Any valid order is accepted. The task graph has no cycle.

Reported variants (not graded)

  • Services hit by an outage: the services depend on each other; some are taken down; which services are affected? This is Part 4. The candidate who got it was allowed to choose the input format and used a map from each service to the services that depend on it.
  • Would this insertion create a cycle? Given a prerequisite DAG, would adding one item with its edges create a loop? Add the edges and run Part 1, or check whether any new edge's target can already reach its source.
  • Install a package with its dependencies: each package has an install() that installs only itself; write installWithDependencies(package) so every dependency is installed first, and detect cyclic dependencies. In the same candidate's onsite, the question was the build order for one target package (it plus everything it needs) with a DFS that keeps a visiting set and a visited set. Not graded; the Solution has a sketch.

Follow-up Arc

Interviewers escalate through these phases. The order varies, but most candidates see at least one from each bucket.
Trade-off discussion · 10
Trade-off discussion

Are there other ways to solve this?

Probes for: Asked before any code in an SDE II phone screen

Is your queue traversal BFS or DFS? Are you sure?

Probes for: After an in-degree queue solution; calling it DFS draws repeated 'Are you sure?'

What are the time and space complexity?

Probes for: After the code; reported in several rounds

Walk me through your code with a small example.

Probes for: After the complexity discussion

What is the difference between BFS and DFS here?

Probes for: New-grad round, training-course catalog validation

Can you return a concrete order, not just true or false?

Probes for: After LeetCode 207

If there is a cycle, can you print the courses on it?

Probes for: After returning the order; one candidate described three-colour DFS but ran out of time to code it

Explain how the visiting and visited sets detect a cycle.

Probes for: Package build-order version, where DFS was required

How do you handle cyclic dependencies?

Probes for: Package install and build-order versions

If some tasks change, which tasks must rerun, including indirectly affected ones, and in what order?

Probes for: SDE II onsite, after the order and cycle detection

What Reports Emphasize

Interviewer hints: In an SDE II phone screen the interviewer asked for other approaches before coding, then asked whether the queue traversal was BFS or DFS; the candidate warns that calling it DFS draws repeated "Are you sure?". The same interviewer asked for time and space complexity and a walk-through on a small example.; In the services-outage version the interviewer had no example prepared and drew a dependency graph when asked, confirmed the input would always be valid (no cycle), and let the candidate choose the input format.; In a package build-order onsite the candidate had to use DFS and explain the visiting set (current path) and the visited set (finished), with O(V + E) time and space.; In a bar-raiser round the follow-ups after LeetCode 207 were a concrete order, then printing the courses on a cycle.

Outcomes: An intern candidate who got a Course Schedule II-like question passed, though time spent settling an OOD question in the same round left almost none to explain the code. An MLE candidate who wrote both BFS and DFS was rejected; HR said the coding feedback was that it needed too many hints, which the candidate disputes. Another candidate who solved the course question onsite failed the loop; a reply in the thread puts it down to the behavioral answers.

Our note (standard advice, not from the reports): Say the edge direction out loud before coding: [a, b] means b comes before a, so the edge is b -> a.; Kahn's algorithm answers Parts 1 and 2 with one loop; a three-colour DFS is the natural tool once the cycle itself is wanted.; A DFS with a single seen set mistakes a diamond for a cycle.; Python's default recursion limit is about 1,000 frames, so prefer an explicit stack for long chains.

Practice

Write your own against 14 test cases, or read the worked solution — approach, complexity, and code that runs.

Prepping for Amazon with friends?

Send them this page. It is free to read, no account needed.

More Amazon Questions

Free preview

Every question in the Amazon catalog gets this depth

What you just read — canonical solution, follow-up arc, what passing candidates actually did — exists for all 57 Amazon questions, refreshed monthly from new candidate reports.

$59/mo — or $50/mo with the 3-month pass · cancel anytime
Amazon · Coding · Reported 16× across candidate reports
Is this helpful?