Graph Interview Questions 2026

BFS, DFS, topological sort, shortest path, connected components: the graph patterns that appear in FAANG interviews, how to recognize them, and what companies are actually asking.

Why Graph Problems Are Frequent

Graph problems appear in 25% of coding interview questions based on LeakCode's data, making them the second most common topic after arrays. They appear frequently because graphs model real-world problems (social networks, maps, dependency systems) and require genuine algorithmic depth. Many problems that look like arrays or trees are actually graphs in disguise.

Google and Meta in particular are known for graph-heavy interviews. Meta candidates report graph problems in over 40% of their coding rounds.

Graph Representations

Know how to work with both adjacency lists and adjacency matrices. In interviews, adjacency lists (dict of lists) are almost always the right representation unless the problem gives you a dense matrix explicitly. Know how to build an adjacency list from an edge list in O(E) time, and how to check edge existence in O(1) with an adjacency matrix.

Grid graphs: a 2D grid is a graph where each cell is a node and edges connect to 4 (or 8) neighbors. BFS/DFS on grids uses the same logic as on explicit graphs. This is the most common form of graph problem in interviews because grids are visually intuitive.

Core Graph Patterns

Connected components
Algorithm: DFS or BFS from unvisited nodes
Examples: Number of islands, friend circles, provinces
Shortest path (unweighted)
Algorithm: BFS from source
Examples: Word ladder, minimum steps in grid, 0-1 BFS
Topological sort
Algorithm: DFS post-order or Kahn's (BFS with indegree)
Examples: Course schedule, task ordering, dependency resolution
Cycle detection
Algorithm: DFS with color states (white/gray/black)
Examples: Detect cycle in directed graph, deadlock detection
Bipartite check
Algorithm: BFS with 2-coloring
Examples: Is graph bipartite, divide team into 2 groups
Union-Find (Disjoint Set)
Algorithm: Path compression + union by rank
Examples: Redundant connection, accounts merge, minimum spanning tree

BFS vs DFS: When to Use Which

Use BFS when: you need shortest path, you need to process nodes level by level, or the solution is near the source. Use DFS when: you are exploring all possibilities (permutations, combinations), you are doing topological sort, or you are detecting cycles. For connected components, both work and the choice is stylistic.

BFS uses a queue (deque in Python). DFS uses recursion or an explicit stack. Both have O(V+E) time complexity on adjacency lists. BFS uses O(V) space in the worst case for the queue. DFS uses O(V) space for the recursion stack (O(H) for balanced structures). On interview problems, state this complexity explicitly.

Browse Real Graph Questions by Company

See actual graph questions asked at FAANG and top tech companies, from verified candidate reports.

Browse Graph Questions