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.
Graph practice questions
Color the nodes in a graph so adjacent nodes always have different colors. keep reading »
MeshMessage »You wrote a trendy new messaging app, MeshMessage, to get around flaky cell phone coverage. But messages are taking too long to send... keep reading »
Recipe Step Order »Put a recipe's steps in an order where every step comes after its prerequisites, or detect that the rules form a cycle. A topological sort question. keep reading »
Cake Delivery Route »Find the fastest route for a bakery's delivery van when roads take different times and some are one-way. A Dijkstra's algorithm question, step by step. keep reading »
Find Repeat, Space Edition BEAST MODE »Figure out which number is repeated. But here's the catch: do it in linear time and constant space! keep reading »
Keep going with the Trees and graphs section of our coding interview course.
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?
What are the most common graph interview questions?
How should I represent a graph in a coding interview?
What is the time complexity of BFS and DFS?
Do I need to know Dijkstra's algorithm for coding interviews?
More interview questions by topic
- Array and string interview questions
- Hash table interview questions
- Greedy algorithm interview questions
- Sorting and binary search interview questions
- Binary tree and BST interview questions
- Dynamic programming and recursion interview questions
- Stack and queue interview questions
- Linked list interview questions
- Bit manipulation interview questions
- System design interview questions
- All coding interview questions
Interview Cake