Graph problems are a hot topic in technical interviews. They test your algorithmic thinking, problem-solving skills, and ability to model real-world problems. Whether youβre facing FAANG interviews or coding challenges on LeetCode, understanding graph algorithms is a must.
In this guide, weβll walk through 12 classic graph problems, along with the core concepts, relevant algorithms, and example use cases.
π 1. DFS & BFS Traversals
Problem: Traverse a graph using DFS and BFS.
Use Cases: Find connected components, detect cycles, search.
β
Algorithms:
- DFS (Recursive or Stack-based)
- BFS (Queue-based)
π Example:
Perform DFS and BFS starting from a given node in an undirected graph.
π 2. Shortest Path Algorithms
Problem: Find the shortest path between nodes.
β
Algorithms:
- Dijkstraβs Algorithm β For non-negative weights
- Bellman-Ford Algorithm β Handles negative weights
- Floyd-Warshall Algorithm β All-pairs shortest path
π Example:
Find the shortest path from a source to all other nodes in a weighted graph.
π§± 3. Topological Sorting
Problem: Linear ordering of vertices in a Directed Acyclic Graph (DAG).
β
Algorithms:
- DFS-based Topological Sort
- Kahnβs Algorithm (BFS)
π Example:
Task scheduling with dependencies: What is the correct order?
π 4. Cycle Detection
Problem: Check if a graph contains any cycles.
β
Algorithms:
- DFS (with parent tracking for undirected graphs)
- Union-Find (Disjoint Set)
- Topological Sort (for directed graphs)
π Example:
Detect if a course prerequisite graph has circular dependencies.
π 5. Strongly Connected Components (SCC)
Problem: Find all SCCs in a directed graph.
β
Algorithms:
- Kosarajuβs Algorithm
- Tarjanβs Algorithm
π Example:
Break a web of links into independent navigable sections.
π² 6. Minimum Spanning Tree (MST)
Problem: Connect all vertices with minimum edge weight.
β
Algorithms:
- Kruskalβs Algorithm (Union-Find)
- Primβs Algorithm (Min Heap)
π Example:
Design a network with minimal wiring cost.
π¨ 7. Graph Coloring
Problem: Color vertices so no adjacent vertices share the same color.
π Example:
Check if a graph is bipartite (2-colorable).
β
Techniques:
- BFS/DFS with color tracking
- Backtracking (for n-color problems)
π§ 8. Maximum Flow (Flow Networks)
Problem: Calculate max flow from source to sink.
β
Algorithms:
- Ford-Fulkerson
- Edmonds-Karp (BFS-based improvement)
π Example:
Optimize water, traffic, or data flow in a network.
π 9. All-Pairs Shortest Path
Problem: Compute shortest paths between all node pairs.
β
Algorithm:
π Example:
Precompute delivery times between all cities.
βοΈ 10. Traveling Salesman Problem (TSP)
Problem: Visit every node exactly once, return to start, and minimize cost.
β
Approaches:
- Dynamic Programming (Exponential)
- Approximation (Genetic Algorithms, Greedy)
π Example:
Route planning for a delivery truck.
π§© 11. Connected Components
Problem: Count or extract all connected components in an undirected graph.
β
Techniques:
π Example:
Count the number of isolated social groups.
π€ 12. Longest Path in DAG
Problem: Find the longest path in a DAG.
β
Algorithm:
- Topological Sort + Dynamic Programming
π Example:
Find the critical path in a project management schedule.
Bonus Interview Questions
π Number of Islands
Given a 2D grid (1 = land, 0 = water), count the number of islands.
Type: Connected component counting
Approach: DFS/BFS
π€ Word Ladder
Transform a word into another by changing one letter at a time using a word list.
Type: Shortest path in unweighted graph
Approach: BFS
π Interview Tips for Graph Problems
β
Know the Algorithms: Understand DFS, BFS, Union-Find, Dijkstra, etc.
β
Practice on Paper: Get used to dry runs, especially for recursion.
β
Use Adjacency Lists: Especially for sparse graphs
β
Analyze Time/Space Complexity: Interviewers love optimization insights
β
Practice Platforms: LeetCode, HackerRank, Codeforces, CodeSignal
Final Thoughts
Graphs unlock a wide world of real-world modeling and algorithmic thinking. Whether youβre scheduling tasks, optimizing routes, or solving puzzles β graph problems are interview gold.