My first instinct was union-find but I defaulted to BFS since I could code it faster under pressure.
Model the graph as an adjacency list and traverse each connected component using BFS or DFS. For each component, track the minimum and maximum node values, then compute the difference and keep the global maximum. This yields O(V + E) time and O(V + E) space.
Pro tip: Mention that you can avoid building an adjacency list by using Union-Find to group nodes, then aggregate min/max per root—this shows awareness of alternative trade-offs and can be more memory-efficient for dense graphs.
Confirm the graph is undirected, nodes are 0-indexed or 1-indexed, and values can be negative. Ask about graph size to choose between BFS/DFS and Union-Find.
Decide between BFS/DFS with an adjacency list or Union-Find. Explain that both achieve O(V+E) time, but Union-Find may simplify component aggregation.
For each unvisited node, start a traversal. During traversal, update the component's min and max values by comparing with each visited node's value.
After finishing a component, compute max - min and update the global answer if larger. Handle isolated nodes (difference 0).
State O(V+E) time and O(V+E) space for BFS/DFS, or O(V+E α(V)) for Union-Find. Discuss empty graph, single node, and negative values.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.