← Amazon Interview Insights

Amazon·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Apr 2026

Summary

Amazon SWE coding round, pretty much a graph theory question the whole way through. They pushed hard on follow-ups so knowing just the basics wasn't enough.

Questions Asked (4)

Q1

Given a directed graph with N nodes and a list of dependency edges, return a valid topological ordering of all nodes, or indicate that no valid ordering exists due to a cycle.

Algorithms & Data Structures
Author's notes

I went with the in-degree queue approach.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem constraints (e.g., graph representation, whether all nodes must be included) and then present a topological sort algorithm such as Kahn's (BFS-based) or DFS-based. Explain how to detect cycles and handle edge cases, and analyze time and space complexity.

Pro tip: Mention that Kahn's algorithm naturally detects cycles by checking if the number of processed nodes equals N, and that it's often preferred for its simplicity and iterative nature. Also, discuss how this applies to real-world dependency resolution like build systems or task scheduling.

1. Clarify requirements and constraints

Ask about graph representation (adjacency list/matrix), whether the graph is guaranteed to be connected, and if all nodes must be included. Confirm that the output should be any valid ordering or a specific one.

2. Choose an algorithm

Select either Kahn's algorithm (BFS with in-degree tracking) or DFS with temporary/permanent marks. Explain the trade-offs: Kahn's is iterative and detects cycles easily; DFS is recursive and may be simpler for some.

3. Walk through the algorithm

Describe step-by-step: compute in-degrees, initialize a queue with nodes of in-degree 0, process nodes while updating in-degrees of neighbors, and enqueue when in-degree becomes 0. For DFS, perform post-order traversal and reverse the result.

4. Handle cycle detection

Explain how to detect a cycle: in Kahn's, if the result size is less than N, a cycle exists; in DFS, if a back edge is found (node in current recursion stack). Return an error or empty list accordingly.

5. Analyze complexity and edge cases

State time complexity O(V+E) and space O(V+E). Discuss edge cases: empty graph, single node, self-loops, disconnected components, and multiple valid orderings.

Key Points to Mention

  • Graph representation: adjacency list for efficient traversal.
  • In-degree calculation and queue initialization in Kahn's algorithm.
  • Cycle detection: comparing processed count to N or using recursion stack in DFS.
  • Time and space complexity: O(V+E) time, O(V+E) space.
  • Handling disconnected graphs: ensure all nodes are processed.
  • Real-world applications: build systems, task scheduling, course prerequisites.

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

Q2

How would you enumerate all valid topological orderings of the graph, not just one?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This tripped me up more than I expected.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Explain that enumerating all topological orderings requires a backtracking approach that explores all valid choices at each step, using indegree tracking to identify available nodes. Emphasize that this is inherently exponential in the worst case, so discuss complexity and potential optimizations or trade-offs.

Pro tip: Mention that the number of topological orderings can be huge, so for large graphs you might need to sample or use dynamic programming to count them instead of enumerating all. Also, clarify that the algorithm can be adapted to find the lexicographically smallest or largest ordering if needed.

1. Clarify the problem and constraints

Confirm that the graph is a DAG and that we need all valid topological orderings, not just one. Discuss input size and whether output size is a concern.

2. Outline backtracking algorithm

Describe maintaining indegree counts and a set of nodes with indegree zero. At each step, pick any such node, add it to the ordering, decrement indegrees of its neighbors, and recurse. Backtrack by restoring state.

3. Analyze complexity and trade-offs

Explain that time complexity is O(V+E) per ordering, but total output can be exponential (up to V! in worst case). Mention that this is unavoidable if all orderings are required.

4. Discuss optimizations and alternatives

For large graphs, suggest counting orderings via DP or sampling. Also mention that if only a subset is needed, we can prune the search or use heuristics.

5. Provide example and edge cases

Walk through a small example (e.g., 4-node DAG) to illustrate. Mention edge cases: disconnected graph, multiple components, and graphs with unique ordering.

Key Points to Mention

  • Backtracking with indegree tracking to identify available nodes
  • Exponential worst-case time complexity (up to V! orderings)
  • Use of recursion and state restoration for backtracking
  • Potential optimizations: pruning, memoization for counting, or sampling
  • Handling disconnected graphs and multiple valid starting points
  • Comparison with standard topological sort (Kahn's algorithm or DFS)

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

Q3

Can you modify the solution to always return the lexicographically smallest valid topological ordering?

Algorithms & Data Structures
Author's notes

Swap the plain queue for a min-heap.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Explain that the standard Kahn's algorithm can be modified by replacing the queue with a min-heap to always extract the smallest available node, ensuring the lexicographically smallest topological order. Then, discuss the time complexity change from O(V+E) to O((V+E) log V) due to heap operations, and mention that this approach is optimal for this problem.

Pro tip: Mention that this modification is a common interview follow-up at Amazon, and that using a min-heap is the canonical solution. Also, note that if the graph is large, the log factor might be a concern, but it's necessary for lexicographical order.

1. Clarify the problem

Confirm that the goal is to return the lexicographically smallest topological ordering among all valid orderings, and that the graph may have multiple valid orderings.

2. Recall standard topological sort

Briefly explain Kahn's algorithm: compute in-degrees, use a queue to process nodes with zero in-degree, and build the order.

3. Modify to use a min-heap

Replace the queue with a min-heap (priority queue) to always extract the smallest node with zero in-degree, ensuring lexicographical order.

4. Analyze complexity

State that the time complexity becomes O((V+E) log V) due to heap operations, and space complexity remains O(V+E).

5. Discuss correctness and edge cases

Explain why the greedy choice of the smallest available node leads to the lexicographically smallest order, and handle cases like cycles (return empty) and disconnected graphs.

Key Points to Mention

  • Kahn's algorithm with in-degree tracking
  • Min-heap (priority queue) to select smallest available node
  • Time complexity: O((V+E) log V)
  • Space complexity: O(V+E)
  • Correctness: greedy choice ensures lexicographically smallest order
  • Handling cycles: if not all nodes are processed, return empty list

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

Q4

How would you group nodes into parallel execution batches, where all nodes in the same batch can run simultaneously?

Algorithms & Data StructuresSystem Design
Author's notes

Nice follow-up.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as a directed acyclic graph (DAG) where nodes represent tasks and edges represent dependencies. Use topological sorting with levelization (e.g., Kahn's algorithm) to assign each node to a batch based on its longest path from any source, ensuring all nodes in a batch have no dependencies on each other. Discuss handling cycles, dynamic graphs, and scalability for large systems.

Pro tip: Mention that this is essentially computing the longest path in a DAG, which can be done in O(V+E) time, and highlight how this approach naturally handles dynamic updates if you maintain in-degrees and levels incrementally.

1. Clarify requirements and assumptions

Ask about graph size, whether it's static or dynamic, if cycles are possible, and if there are constraints like resource limits per batch. This shows you consider real-world scenarios.

2. Model as a DAG and define batches

Represent nodes as tasks and edges as dependencies. Define a batch as a set of nodes with no incoming edges from nodes in the same or later batches, ensuring all can run in parallel.

3. Choose an algorithm (topological sort with levels)

Use Kahn's algorithm: compute in-degrees, process nodes with zero in-degree, assign them to the current batch, then decrement in-degrees of neighbors. Repeat until all nodes are processed.

4. Analyze complexity and edge cases

Time complexity is O(V+E). Handle cycles by detecting if not all nodes are processed; discuss fallback (e.g., error or break cycle). Consider disconnected components and isolated nodes.

5. Discuss optimizations and extensions

For dynamic graphs, maintain levels incrementally. For large-scale systems, consider distributed processing or streaming algorithms. Mention potential for parallelizing the algorithm itself.

Key Points to Mention

  • Topological sorting with levelization (Kahn's algorithm or DFS-based longest path)
  • Batch assignment based on longest path from source (level = max(level of predecessors) + 1)
  • Time and space complexity: O(V+E) time, O(V) space
  • Cycle detection and handling (e.g., if processed nodes < total nodes, there's a cycle)
  • Applicability to dynamic graphs and incremental updates
  • Real-world use cases like build systems, task scheduling, and data pipelines

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