← Amazon Interview Insights

Amazon·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Amazon SWE interview with a graph theory question that seems deceptively simple but has a few more angles than you'd expect.

Questions Asked (1)

Q1

How can you determine that a given graph is not a tree?

Algorithms & Data Structures
Author's notes

I started rattling off the obvious stuff about cycles and then kind of stumbled when I realized there are multiple ways a graph fails to be a tree.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by defining the necessary and sufficient conditions for a graph to be a tree: connected and acyclic, with exactly n-1 edges. Then explain how to check each condition using algorithms like BFS/DFS for connectivity and cycle detection, or edge counting. Finally, discuss the trade-offs and edge cases.

Pro tip: Mention that for an undirected graph, if it's connected and has exactly n-1 edges, it's automatically a tree; this avoids explicit cycle detection. Also, highlight that for directed graphs, you need to check that the underlying undirected graph is a tree and that there's a unique root with all nodes reachable.

1. Define tree properties

State that a tree is a connected acyclic graph with exactly n-1 edges. Emphasize that all three conditions are equivalent for undirected graphs.

2. Check connectivity

Use BFS or DFS from any node to see if all nodes are visited. If not, the graph is disconnected and thus not a tree.

3. Check for cycles

During BFS/DFS, detect back edges. Alternatively, if the graph is connected, just count edges: if edges != n-1, it's not a tree.

4. Handle directed graphs

For directed graphs, first check that the underlying undirected graph is a tree, then ensure exactly one node has indegree 0 (root) and all others have indegree 1, and all nodes are reachable from the root.

5. Discuss edge cases and complexity

Mention special cases like empty graph, single node, or multiple components. Note that the checks can be done in O(V+E) time and O(V) space.

Key Points to Mention

  • A tree must be connected and acyclic, and have exactly n-1 edges.
  • For undirected graphs, connectivity plus n-1 edges implies acyclic.
  • Cycle detection can be done via DFS (back edges) or Union-Find.
  • For directed graphs, check underlying undirected tree and unique root with indegree 0.
  • Time complexity: O(V+E) for BFS/DFS, O(E α(V)) for Union-Find.
  • Edge cases: empty graph, single node, self-loops, multiple edges.

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