← Amazon Interview Insights

Amazon·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Amazon SWE phone screen that came down to a union-find problem. The product-merge framing threw me a bit but the underlying algorithm is the same old connected components stuff.

Questions Asked (3)

Q1

Given a list of product pairs where each pair means the two products share a category, count the total number of distinct categories and the size of each at the end.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I went with BFS flood-fill because I can write that cleanly under pressure and it passed.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the products as nodes in a graph where each pair represents an edge, then use Union-Find (Disjoint Set Union) to efficiently merge products into categories. After processing all pairs, count the distinct roots and their sizes to get the number of categories and their sizes.

Pro tip: Mention that Union-Find with path compression and union by rank achieves near O(1) amortized time per operation, making it optimal for large datasets. Also, clarify how you handle products that appear in no pairs—they form singleton categories.

1. Clarify the problem

Confirm that each pair indicates the two products belong to the same category, and that categories are transitive (if A shares with B and B with C, then A, B, and C are in the same category). Ask if products not appearing in any pair should be counted as individual categories.

2. Choose the right data structure

Select Union-Find (Disjoint Set Union) to efficiently manage merging of categories. Explain that it supports union and find operations in nearly constant amortized time with path compression and union by rank.

3. Process all pairs

Iterate through each product pair, perform union on the two products, and ensure all products are added to the Union-Find structure (including those not in any pair).

4. Count categories and sizes

After processing, traverse all products, find their root, and tally the size of each root. The number of distinct roots is the number of categories, and the tally gives the size of each.

5. Analyze complexity and edge cases

State that time complexity is O(N α(N)) where N is the number of products, and space is O(N). Discuss edge cases like empty input, duplicate pairs, and self-pairs.

Key Points to Mention

  • Union-Find (Disjoint Set Union) with path compression and union by rank for optimal performance.
  • Transitivity of category sharing: if A shares with B and B with C, then A, B, and C are in the same category.
  • Handling products that appear in no pairs as singleton categories.
  • Time complexity: O(N α(N)) amortized, which is effectively linear.
  • Space complexity: O(N) for the Union-Find structure.
  • Alternative approaches like BFS/DFS on a graph, but Union-Find is more efficient for dynamic connectivity.

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

Q2

Given n nodes and an edge list, count the number of connected components in the graph.

Algorithms & Data Structures
Author's notes

Straightforward version of the same problem.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the graph type (directed vs. undirected) and constraints, then propose either Union-Find or DFS/BFS as the core algorithm. Walk through the chosen approach step-by-step, analyze time and space complexity, and discuss edge cases like isolated nodes or empty graphs.

Pro tip: Mention that Union-Find with path compression and union by rank is often preferred for dynamic graphs or when edges are streamed, while DFS/BFS is simpler for static graphs. This shows you consider trade-offs beyond just correctness.

1. Clarify the problem

Confirm whether the graph is directed or undirected, if nodes are labeled 0 to n-1, and if there are any constraints on edge count or memory.

2. Choose an algorithm

Select Union-Find (disjoint set) or graph traversal (DFS/BFS) based on graph properties and constraints. Explain why.

3. Outline the algorithm

Describe the steps: initialize data structures, process edges or traverse, and count components. For Union-Find, detail union and find operations.

4. Analyze complexity

State time and space complexity. For Union-Find with optimizations, it's nearly O(E) time and O(n) space; for DFS/BFS, O(n+E) time and O(n) space.

5. Handle edge cases

Discuss cases like no edges (n components), complete graph (1 component), disconnected nodes, and potential recursion depth issues with DFS.

Key Points to Mention

  • Union-Find with path compression and union by rank for near-constant time operations
  • DFS/BFS traversal using adjacency list representation
  • Time complexity: O(n + E) for traversal, O(E α(n)) for Union-Find
  • Space complexity: O(n) for visited array or parent array
  • Handling isolated nodes and empty edge list
  • Trade-offs: Union-Find better for dynamic connectivity, DFS/BFS simpler for static graphs

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

Q3

How would you detect whether adding a new edge to a graph creates a cycle?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This was a follow-up and I fumbled for a second.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify whether the graph is directed or undirected, as the cycle detection method differs. Then explain the appropriate algorithm: for undirected graphs, use Union-Find to check if the edge connects two vertices already in the same component; for directed graphs, use DFS to check if there's a path from the target to the source. Discuss time and space complexity and potential optimizations.

Pro tip: Mention that for undirected graphs, Union-Find with path compression and union by rank gives near-constant time per operation, making it ideal for dynamic edge additions. For directed graphs, note that DFS can be optimized with iterative implementation to avoid recursion limits.

1. Clarify graph type and constraints

Ask if the graph is directed or undirected, and whether it's static or dynamic. This determines the algorithm choice.

2. For undirected graphs: Union-Find approach

Maintain disjoint sets of vertices. For a new edge (u, v), check if u and v are in the same set; if yes, adding the edge creates a cycle. Otherwise, union the sets.

3. For directed graphs: DFS/BFS approach

To check if adding edge (u, v) creates a cycle, check if there is already a path from v to u. If yes, adding (u, v) creates a cycle. Use DFS or BFS from v to see if u is reachable.

4. Analyze complexity and trade-offs

Discuss time and space complexity: Union-Find with path compression and union by rank is nearly O(1) per operation; DFS is O(V+E) per query. Mention trade-offs for dynamic graphs.

5. Consider optimizations and edge cases

For multiple edge additions, consider incremental algorithms or maintaining a topological order for directed graphs. Handle self-loops and parallel edges.

Key Points to Mention

  • Union-Find (Disjoint Set Union) with path compression and union by rank for undirected graphs
  • DFS or BFS to check reachability for directed graphs
  • Time complexity: near O(1) per operation for Union-Find, O(V+E) for DFS
  • Space complexity: O(V) for Union-Find, O(V) for DFS recursion stack
  • Handling of self-loops and multiple edges
  • Trade-offs between online (incremental) and offline algorithms

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