Skip to content
Learn Netverks
Company prep Amazon
Junior (1–3 years) Coding / DSA Medium

Can all courses be finished given prerequisites?

Reported in Amazon interview loops. Topological sort and directed cycle detection interview staple.

Role
SDE
Location
Ahmedabad

Context for Amazon candidates:

Each prerequisite pair means one course depends on another. Return whether all courses can be completed.

Try answering aloud first

Cover trade-offs, structure, and a concrete example before revealing the baseline response.

Spoiler-free prep mode

How to frame this at Amazon: Connect your answer to measurable impact, clarity of thought, and trade-offs the team cares about. Below is a strong baseline response you can adapt with your own project examples.

Model courses as a directed graph and detect cycles. If a cycle exists, dependency order is impossible and answer is false.

Two standard approaches: DFS with recursion states (unvisited, visiting, visited) or Kahn algorithm using indegree queue. Kahn is iterative and easier to explain when interviewers ask for actual ordering too.

Complexity is O(V + E), where V is number of courses and E is prerequisites.

Comments (0)

Share how this question came up in your loop, or add tips for others preparing.

Log in to comment on this question.