← Amazon Interview Insights

Amazon·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
May 2026Remote

Summary

Amazon SWE interview that went pretty deep into graph algorithms. Started with a classic linked list problem but the follow-up kept going until we were talking about distributed systems and memory constraints, which I was not fully prepared for.

Questions Asked (2)

Q1

Given a singly linked list, determine if it contains a cycle. Implement a solution using O(1) extra space and explain why it works.

Algorithms & Data Structures
Author's notes

Floyd's two-pointer thing, slow and fast pointer.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem and constraints, then propose Floyd's cycle-finding algorithm (tortoise and hare) using two pointers moving at different speeds. Explain how the pointers will eventually meet if a cycle exists, and why the algorithm uses O(1) extra space.

Pro tip: Mention that Floyd's algorithm not only detects cycles but can also find the start of the cycle, which is often a follow-up question. Also, discuss edge cases like empty list or single node without cycle.

1. Clarify the problem

Ask if the linked list can be modified, if there are constraints on time complexity, and confirm that O(1) space means no additional data structures like hash sets.

2. Explain the approach

Describe using two pointers: slow moves one step, fast moves two steps. If they meet, there is a cycle; if fast reaches null, no cycle.

3. Implement the solution

Write clean code with a while loop that advances slow and fast pointers, checking for null and equality. Handle edge cases like empty list or single node.

4. Explain why it works

Justify that if a cycle exists, the fast pointer will eventually catch up to the slow pointer inside the cycle due to relative speed difference of 1.

5. Analyze complexity

State that time complexity is O(n) and space complexity is O(1), and discuss why the algorithm is optimal for this problem.

Key Points to Mention

  • Floyd's cycle-finding algorithm (tortoise and hare)
  • Two pointers moving at different speeds
  • Meeting point indicates a cycle
  • O(1) space complexity because only two pointers are used
  • Time complexity O(n) with proof that fast pointer catches slow in at most n steps
  • Edge cases: empty list, single node, cycle at head

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

Q2

Generalize the cycle detection problem to a directed graph where nodes can have multiple children. Given a starting node, determine if any cycle is reachable from it. How would you handle disconnected graphs, graphs too large to fit in memory, and what are the time and space complexity trade-offs?

Algorithms & Data StructuresSystem DesignTechnical Trade-offs
Author's notes

This is where things got uncomfortable.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by explaining the standard DFS-based cycle detection using a recursion stack or color marking, then generalize to directed graphs with multiple children. Address the follow-up scenarios (disconnected graphs, large graphs) by extending the algorithm with techniques like iterative DFS, external memory, or distributed processing, and discuss trade-offs.

Pro tip: Emphasize that cycle detection in directed graphs requires tracking the current recursion stack (or using three colors) to avoid false positives from cross edges. For large graphs, mention that iterative DFS with explicit stack avoids recursion depth limits and can be adapted for external memory.

1. Clarify the problem and assumptions

Restate the problem: given a starting node in a directed graph, determine if any cycle is reachable. Clarify whether the graph is static, if nodes have multiple children (out-degree > 1), and if the graph is connected or not.

2. Present the core algorithm

Describe DFS with a recursion stack (or three-color marking: white, gray, black) to detect cycles. Explain that a back edge to a gray node indicates a cycle. Mention that BFS with topological sorting (Kahn's algorithm) can also detect cycles but may be less efficient for reachability from a single node.

3. Handle disconnected graphs

If the graph is disconnected, run the cycle detection from the given starting node only. If the question implies checking the entire graph, iterate over all unvisited nodes. But since the question specifies 'reachable from it', focus on the starting node's connected component.

4. Address large graphs that don't fit in memory

Discuss strategies: use external memory algorithms (e.g., sort-based or streaming), distributed processing (e.g., MapReduce, Pregel), or iterative deepening DFS with disk-based storage. Mention that cycle detection can be done via repeated reachability queries or by computing strongly connected components (SCCs) using algorithms like Tarjan's or Kosaraju's, which can be adapted for external memory.

5. Analyze time and space complexity trade-offs

For in-memory DFS: O(V+E) time, O(V) space for recursion stack and visited set. For large graphs: external memory algorithms may increase I/O complexity; distributed algorithms add communication overhead. Discuss trade-offs between time, space, and scalability.

Key Points to Mention

  • DFS with recursion stack (or three-color marking) is the standard for directed cycle detection; BFS with topological sort also works but may not be optimal for single-source reachability.
  • For disconnected graphs, only traverse from the given starting node; if the entire graph must be checked, iterate over all components.
  • For graphs too large for memory, consider external memory algorithms (e.g., sort-based), distributed processing (e.g., Pregel, MapReduce), or SCC-based approaches.
  • Time complexity: O(V+E) for in-memory DFS; space complexity: O(V) for visited set and recursion stack. For large graphs, space can be reduced by iterative DFS with explicit stack, but may still be O(V).
  • Trade-offs: external memory algorithms increase I/O but handle large graphs; distributed algorithms scale but add communication overhead; SCC-based methods can detect cycles in the entire graph but may be overkill for single-source reachability.
  • Mention that recursion depth can be an issue for deep graphs; iterative DFS avoids stack overflow.

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