Graph Interview Questions

How to approach graph questions

Graph questions often don't say "graph." They describe a network of users, a map of routes, a list of tasks that depend on each other, or a grid, and part of the test is noticing that you're looking at nodes connected by edges. Once you see the graph, decide how to store it (usually an adjacency list) and how to walk it.

Breadth-first search (BFS) explores a graph level by level using a queue, so in a graph without edge weights it finds the shortest path. Depth-first search (DFS) goes as deep as it can before backtracking, which suits questions about connectivity, cycles and ordering, like topological sort. When edges have weights, finding the shortest path calls for Dijkstra's algorithm.

Either way, keep track of the nodes you've already visited so you don't loop forever around a cycle, and give your complexity in terms of both nodes and edges: BFS and DFS on an adjacency list take O(V + E) time.

Interview Cake isn't just another question database. Each question below walks you through the problem step by step, with hints and explanations as you need them, just like a real interviewer.

Concepts to review

  • Graphs: Directed vs. undirected, weighted, cyclic vs. acyclic: the vocabulary interviewers expect.
  • Adjacency lists: The usual way to store a graph in an interview: each node maps to a list of its neighbors.
  • Breadth-first search (BFS): Level-by-level search with a queue, which finds shortest paths in unweighted graphs.
  • Depth-first search (DFS): Go deep, then backtrack: good for connectivity, cycle detection and exhaustive search.
  • Topological sort: Ordering the nodes of a directed acyclic graph so every edge points forward, as with task dependencies.
  • Dijkstra's algorithm: Shortest paths when edges have non-negative weights.

Quick reference: Big O cheat sheet

Graph interview FAQ

When should I use BFS vs. DFS?

Use BFS when you need the shortest path in an unweighted graph, or want to visit nodes in order of their distance from the start, since it explores level by level. Use DFS when you need to explore whole paths, detect a cycle, or find a topological ordering. Both take O(V + E) time on an adjacency list, so when either one works, pick the one that's simpler to code.

What are the most common graph interview questions?

Common ones include finding the shortest path between two nodes, counting connected components or islands in a grid, detecting a cycle, ordering tasks that depend on each other (topological sort), copying a graph, and coloring a graph so that no two neighbors share a color.

How should I represent a graph in a coding interview?

An adjacency list, such as a hash map from each node to a list of its neighbors, is the usual choice: it takes O(V + E) space and makes it fast to loop over a node's neighbors. An adjacency matrix takes O(V²) space but answers "is there an edge between these two nodes?" in O(1) time, so it suits dense graphs.

What is the time complexity of BFS and DFS?

O(V + E) on an adjacency list, where V is the number of nodes and E is the number of edges, since each node and each edge is handled a constant number of times. On an adjacency matrix it's O(V²), because finding a node's neighbors means scanning a whole row.

Do I need to know Dijkstra's algorithm for coding interviews?

It's worth knowing for questions about shortest paths in a graph whose edges have weights, where BFS no longer gives the right answer. Know what it does, that it requires non-negative edge weights, and that with a binary heap it runs in O((V + E) log V) time.
. . .