← Amazon Interview Insights

Amazon·Software Engineer·Technical Phone Screen·Junior

Junior
May 2026

Summary

Amazon SWE coding round, one question, classic graph problem. Nothing too wild but apparently a lot of intern candidates fumble this topic so worth knowing cold.

Questions Asked (1)

Q1

Given a list of nodes and edges, find the number of connected components in an undirected graph.

Algorithms & Data Structures
Author's notes

Union-Find is the move here.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem constraints and edge cases, then explain two standard approaches: Union-Find (Disjoint Set Union) and DFS/BFS. Walk through the chosen algorithm step-by-step, analyze time and space complexity, and discuss trade-offs and potential optimizations.

Pro tip: At Amazon, emphasize scalability and real-world application: mention that Union-Find with path compression and union by rank is often preferred for dynamic graphs, and discuss how you'd handle large inputs or streaming data.

1. Clarify the problem

Ask about input format (adjacency list or edge list), graph size, whether nodes are labeled 0 to n-1, and if the graph can have self-loops or multiple edges. Confirm that connected components are defined for undirected graphs.

2. Choose an approach

Select either Union-Find or DFS/BFS based on constraints. Union-Find is efficient for dynamic connectivity and large graphs; DFS/BFS is simpler for static graphs and easier to implement recursively or iteratively.

3. Explain the algorithm

For Union-Find: initialize parent and rank arrays, union all edges, then count unique roots. For DFS/BFS: build adjacency list, iterate through all nodes, and for each unvisited node, increment count and traverse its component.

4. Analyze complexity

State time and space complexity. Union-Find with path compression and union by rank: O(E α(V)) time, O(V) space. DFS/BFS: O(V + E) time, O(V + E) space for adjacency list.

5. Discuss trade-offs and optimizations

Compare approaches: Union-Find is better for dynamic graphs and can be more space-efficient; DFS/BFS may be simpler and can provide additional information like component sizes. Mention potential optimizations like early termination or parallelization for very large graphs.

Key Points to Mention

  • Union-Find (Disjoint Set Union) with path compression and union by rank
  • DFS/BFS traversal using adjacency list
  • Time and space complexity analysis
  • Handling edge cases: empty graph, single node, disconnected nodes, self-loops
  • Trade-offs between Union-Find and DFS/BFS
  • Scalability considerations for large graphs (e.g., distributed processing)

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