← Amazon Interview Insights

Amazon·Machine Learning Engineer·Technical Phone Screen·Senior

SeniorPrefer not to say
Jun 2026

Summary

Interviewed for an MLE role at Amazon and got a graph/topological sort problem. Pretty standard for the level but the cycle detection part tripped me up more than I expected.

Questions Asked (1)

Q1

Given a list of courses and their prerequisites, return a valid order to complete all courses. If a cycle exists making it impossible, return an empty array.

Algorithms & Data Structures
Author's notes

I knew this was topological sort the second I read it, but I fumbled the cycle detection part.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the courses and prerequisites as a directed graph and perform a topological sort using Kahn's algorithm (BFS) or DFS. If the sort processes all nodes, return the order; otherwise, a cycle exists, so return an empty array.

Pro tip: Explicitly state that you would clarify input format (e.g., edge list vs. adjacency list) and discuss trade-offs between Kahn's algorithm and DFS, showing you consider real-world engineering constraints.

1. Clarify and model the problem

Confirm the input format (e.g., number of courses and list of prerequisite pairs) and represent the courses as a directed graph where an edge u→v means u must be taken before v.

2. Choose an algorithm

Select topological sort: either Kahn's algorithm (BFS with in-degree tracking) or DFS with cycle detection. Mention that both have O(V+E) time complexity.

3. Implement the algorithm

For Kahn's: compute in-degrees, enqueue nodes with in-degree 0, then repeatedly dequeue and reduce in-degrees of neighbors. For DFS: perform depth-first search, tracking visited and recursion stack to detect cycles.

4. Handle cycles and return result

If the topological order contains all courses, return it; otherwise, return an empty array to indicate impossibility due to a cycle.

5. Analyze complexity and edge cases

State time and space complexity (O(V+E) time, O(V+E) space) and discuss edge cases like no prerequisites, disconnected components, or self-loops.

Key Points to Mention

  • Graph representation: adjacency list for efficiency
  • Topological sorting algorithms: Kahn's (BFS) and DFS-based
  • Cycle detection: in-degree count or recursion stack
  • Time and space complexity: O(V+E) time, O(V+E) space
  • Edge cases: empty input, no prerequisites, multiple valid orders
  • Real-world relevance: course scheduling, dependency resolution in ML pipelines

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.