SLIDE 22: Section Intro, Graph Patterns

Section 2: Graph Patterns

Trees, which we covered in Section 1, are a special case of graphs. A tree is connected, acyclic, and has a single root. Graphs remove all three constraints. They can be disconnected (multiple separate components). They can have cycles (node A reaches node B reaches node A). And they have no designated root. These differences mean that the traversal techniques from trees need three upgrades: a visited set to avoid infinite loops in cycles, handling of multiple components, and awareness of edge direction.

Graph problems are among the most feared in coding interviews, but they follow a small set of repeating patterns. If you can model the problem as a graph, choose the right traversal, and avoid revisiting nodes, you can solve most graph interview questions.

What We’ll Cover:

  1. Graph representations
  2. BFS vs DFS for graphs
  3. The visited set
  4. Grid as graph
  5. Connected components
  6. Multi-source BFS
  7. Cycle detection
  8. Topological sort
  9. Union-Find

Where This Appears in Interviews:

Company Type How This Section Appears
FAANG / Big Tech Number of Islands, Course Schedule, and Accounts Merge are top-20 most-asked
Mid-size companies Grid traversal and basic graph BFS/DFS appear in phone screens
Startups Dependency resolution, social network connections, and shortest path problems
Online assessments Grid problems and topological sort are common timed questions

Core Idea for This Section: Graph problems are about modeling relationships, choosing a traversal, and preventing repeated work with a visited set.


SLIDE 23: Graph Representations, Adjacency List and Matrix