Graph Interview Questions: Complete Guide (2026)

Which graph algorithms and problem categories appear most at top companies, how to approach graph problems systematically, and how to use real data from LeakCode to prepare.

Why graph problems are critical for top-company interviews

Graphs are the most versatile data structure in computer science. They model networks, dependencies, geographic data, social connections, and state machines. Because of this breadth, graph problems appear at every level of the software engineering interview process at FAANG companies.

According to real interview reports in the LeakCode database, graph questions appear in approximately 20% of coding rounds at top tech companies. Amazon, Google, and Microsoft all ask graph problems at a high rate. At Amazon, graph problems often appear in disguise as dependency resolution, path finding in a warehouse context, or network connectivity problems.

The breadth of graph problems can feel overwhelming, but the same core algorithms handle the vast majority. LeakCode's data from 51,000+ real interview reports shows which categories are most common at each company, so you can prioritize effectively.

Core graph problem categories

Connected components and flood fill

Count or identify distinct connected regions in a grid or graph. The "number of islands" pattern. Solve with either BFS or DFS. Mark visited nodes to avoid cycles. The most commonly reported graph category in LeakCode's database, appearing across all levels and companies.

Shortest path in unweighted graphs

BFS gives the shortest path in terms of edge count in unweighted graphs. Common in grid problems where you want the minimum number of steps to reach a target, word ladder transformations, or social network distance. The key insight: BFS explores nodes level by level, so the first time you reach a node is via the shortest path.

Cycle detection

Detecting whether a directed or undirected graph contains a cycle. For undirected graphs, use Union-Find or DFS with a visited set. For directed graphs, use DFS with a "currently in recursion stack" set (three-color marking). Cycle detection underpins many dependency validation problems.

Topological sort

Ordering nodes in a directed acyclic graph such that every directed edge goes from earlier to later in the ordering. Solves course scheduling, build dependency ordering, and compilation sequencing problems. Know both Kahn's algorithm (BFS with in-degree tracking) and the DFS post-order approach.

Weighted shortest path (Dijkstra)

Dijkstra's algorithm finds the shortest path in a weighted graph with non-negative edge weights. Implemented with a min-heap (priority queue). Appears at Google and in senior-level rounds at other companies. Know the time complexity: O((V + E) log V) with a binary heap.

Union-Find (Disjoint Set Union)

Data structure for tracking connected components with near-O(1) union and find operations. Useful for Kruskal's minimum spanning tree, detecting cycles in undirected graphs, and grouping problems. Know path compression and union by rank optimizations.

Bipartite graph checking

Determine whether a graph can be 2-colored such that no two adjacent nodes have the same color. Equivalent to checking whether the graph contains no odd-length cycles. Solved with BFS or DFS, alternating colors. Appears in scheduling and matching problems.

How to represent a graph in interview code

Use an adjacency list for almost all interview problems. An adjacency list stores the graph as a map from each node to a list of its neighbors. This is O(V + E) space and gives O(degree) neighbor lookup, which is what BFS and DFS need.

For grid problems, you do not need to build an explicit graph. Treat each cell as a node and use the four directional neighbors as its edges. Keep a visited set or modify the grid in-place to mark visited cells.

Always track visited nodes in a set to avoid infinite loops in cyclic graphs. In BFS, add nodes to the visited set when they are enqueued, not when they are dequeued, to avoid redundant processing.

Top companies that ask graph questions

Search Graph Questions on LeakCode

LeakCode has 51,000+ real interview questions from 2,000+ companies. Filter by topic and company to see which graph algorithms your target company asks most.

Browse Graph Questions

How LeakCode helps with graph interview prep

LeakCode indexes real graph interview questions from 1Point3Acres, Blind, LeetCode Discuss, Glassdoor, and Reddit. Filter by company and topic to see which graph categories appear most in recent interview reports at your target company.

See also: how LeakCode works, our data sources, FAQ. Related: binary tree interview questions and dynamic programming interview questions.

Graph algorithm patterns by interview frequency

BFS and DFS together cover roughly 60% of graph interview questions. BFS is the right reach for shortest-path-in-unweighted-graph, level-by-level processing, and finding the connected component containing a starting node. DFS is the right reach for cycle detection, topological sort, and counting connected components. Both run in O(V+E) on adjacency lists; the choice between them is usually stylistic except for shortest-path problems.

Topological sort (Kahn's algorithm or DFS post-order) is heavily probed in problems framed as "course schedule" or "task dependencies." Union-Find appears in dynamic connectivity problems, redundant connection detection, and minimum-spanning-tree variants. Dijkstra's algorithm with a min-heap handles weighted shortest paths. At L5+ levels, you may also see Bellman-Ford (negative weights) or A* (heuristic search) in specific domains like routing.

Common graph problem traps

The most frequent bug under pressure: not tracking visited nodes in BFS or DFS, causing infinite loops or exponential time blowup on cyclic graphs. Always initialize a visited set or array; mark nodes when you push (BFS) or when you enter (DFS). For trees specifically, you can skip the visited check, but for arbitrary graphs you cannot.

The second frequent bug: not handling disconnected components. If the problem asks about "all nodes" but you only start from a single node, you miss the disconnected components. The fix: wrap your BFS/DFS in an outer loop that starts from every unvisited node. Reports on LeakCode tag this as the most common edge-case failure at Google and Meta L4 graph rounds.