Common Patterns
Graph Search & Pathfinding
BFS, DFS, Dijkstra's, and A* — why graph traversal shows up constantly in AI coding problems and how to recognize when you need it.
Graph problems are everywhere in AI coding interviews: navigation between waypoints, network packet routing, dependency resolution, social network analysis, and game state exploration. If you zoom out, a surprising number of problems reduce to "find a path through a graph," and from there the solution follows directly.
Your trusty AI can write any of these algorithms in seconds. What interviewers care about is whether you can spot that a problem is a graph problem in the first place, pick a sensible traversal, explain to them what the algorithm is doing, and steer the AI through the back-and-forth without getting lost.
Recognizing the pattern
Many graph problems hide their structure inside a described process: finding the shortest sequence of word transformations, navigating a maze, propagating a state through a network. The interviewer is watching whether you can see the graph underneath.
Tells that you're looking at a graph problem:
- "Things" and "connections between things." Any time the input has entities and a relationship between them (people and friendships, cities and roads, courses and prerequisites, files and imports), you can model it as a graph. The question is whether you should.
- Shortest, fewest, minimum, or "any path between." These are pathfinding questions. Add weights to the edges and you're in Dijkstra's territory; without weights, BFS.
- Reachability or connectivity. "Can X reach Y?" "How many components are there?" "Is this network fully connected?" These are traversal questions, so BFS or DFS.
- 2D grids with movement rules. A grid is a graph in disguise where each cell connects to its 4 or 8 neighbors. Almost every "rooms," "islands," "maze," or "minimum steps" grid problem reduces to BFS or DFS on this implicit graph.
- A sequence of transformations. Word ladders, state-space search, game playing. Each state is a node; each legal transformation is an edge. Once you frame it that way, the algorithm choice follows.
When you spot one, say it out loud before you start coding: "This is a shortest-path problem on an unweighted graph, so I'm thinking BFS." That's the signal interviewers are reading for.
What is a graph?
Graph traversal algorithms
BFS: Breadth-First Search
DFS: Depth-First Search
Weighted graphs and Dijkstra's algorithm
A*: Dijkstra's with a heuristic
Prompting the AI
Verifying the AI's code
When to use vs. alternatives
What interviewers expect
Putting it together
Purchase Premium to Keep Reading
Unlock this article and so much more with Hello Interview Premium
Reading Progress
On This Page