Skip to content

Resource · Guide or article

Graph cheatsheet for coding interviews

One of the longer cheatsheets in the set, aimed at graph traversal questions, including the frequent case where the graph arrives as a 2D grid. Expect Python code and assume you know basic recursion and queues.

Reviewed

Open the original: Graph cheatsheet for coding interviews (opens the original site in a new tab)

Practice this topicOpens the practice interviewer with a question on Graphs.

Add to a roadmap

Keep this resource with a roadmap you follow. It appears as your own extra resource; the roadmap's steps don't change and it doesn't count toward your tracked steps.

Source content: English

A graph is a structure containing a set of objects (nodes or vertices) where there can be edges between these nodes/vertices. Edges can be directed or undirected and can optionally have values (a weighted graph). Trees are undirected graphs in which any two vertices are connected by exactly one edge and there can be no cycles in the graph.

PrepKit overview

PrepKit editors wrote and reviewed this overview from the source repository. It is not text from the resource itself.

Suited to
The source does not state a level
Format
Cheatsheet

What it covers

  • Compares adjacency matrix, adjacency list, and a hash table of hash tables, calling the last the simplest in interviews.
  • Python DFS and BFS templates for a matrix with a visited set, direction tuples, and bounds checks; BFS uses deque for O(1) dequeue.
  • Ranks algorithms by how often they come up: BFS and DFS common, topological sort and Dijkstra uncommon, Bellman-Ford, Floyd-Warshall, Prim, and Kruskal almost never.
  • An in-degree based topological sort with a sample input and output, returning None when a cycle blocks a full ordering.
  • Corner cases: empty graph, one or two nodes, disconnected parts, and cycles.

How to use it

PrepKit study advice, not a claim made by the source.

Type the matrix DFS template without looking, then convert it to an iterative version with an explicit stack and confirm both visit the same cells. Trace the topological sort example's in-degree counts by hand until your result matches the printed order.

Scope and limitations

Python only. The frequency ranking is the author's view, not measured data. Dijkstra is named but not explained. Several recommended problems are marked LeetCode Premium. Ends with an imported Recommended courses block whose links are not in the recorded file, so PrepKit has not reviewed them.

Access and format

Web page on techinterviewhandbook.org, free to read online in English; the text comes from the MIT-licensed yangshun/tech-interview-handbook repository.

Reviewed by PrepKit on Oct 2, 2026. Listing source commit: e1d28e8886c0

Evidence behind this overview
  • apps/website/contents/algorithms/graph.md

Related preparation

These are PrepKit study suggestions, not employer endorsements or predictions of interview questions.

Pages that include this

Get the PrepKit newsletter

Optional. You don't need it to use PrepKit.

How we handle your email