LearnThatStack Ace your next interview
Computer Science Fundamentals
Graphs.
Change topic Change
Practice · Questions

All questions

Showing of 60
Beginner 14
01

What is a graph, and what real-world problems does it model

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

A graph is a set of objects plus the relationships between them. Whenever your problem is really about connections, a graph is the natural fit. The objects become nodes, and each relationship becomes a link joining two of them.

It models more than you might expect:

  • Road maps, where intersections connect by streets
  • Social networks, where people connect by friendship
  • The web, where pages connect by hyperlinks
  • Task schedules, where jobs connect by "must run before"

The payoff is that one toolbox then applies everywhere. Questions like "can I reach here from there" or "what is the cheapest route" become the same search over any of these. You stop writing bespoke logic per domain and reuse proven traversal instead. That reuse is why graphs show up in routing, package managers, and recommendation systems alike.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

02

What are vertices, edges, degree, and neighbors in a graph

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

Vertices are the objects in a graph, and edges are the connections joining pairs of them. People often call vertices nodes and edges links; the words are interchangeable. A single edge always ties exactly two vertices together.

Two more terms describe local structure. The neighbors of a vertex are the vertices directly joined to it by an edge. The degree of a vertex is how many edges touch it, which equals its neighbor count in a simple graph.

Why care? Degree tells you how connected something is. A social account with degree ten million is a hub you must handle carefully. Neighbors are exactly what every traversal reads next, so representing them cheaply drives your whole performance. When someone calls a graph sparse, they mean average degree stays small, and most vertices have few neighbors.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

03

How do directed and undirected graphs differ, and when use each

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

Edges either point one way or go both ways, and that choice is the whole distinction. In an undirected graph an edge means a mutual relationship, like two friends. In a directed graph an edge goes from one vertex to another, like a one-way street or a follow.

The practical effect shows up in reachability. Undirected edges let you travel from either end to the other. Directed edges only let you move along the arrow, so A reaching B never guarantees B reaches A.

Pick based on whether your relationship is symmetric.

  • Undirected: friendship, physical roads that run both ways, network cables
  • Directed: follows, task ordering, web links, dependencies

Getting this wrong quietly breaks logic. If you model "depends on" as undirected, a topological sort becomes meaningless, and cycle checks stop making sense. Match the edge type to the real relationship first.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

04

What does it mean for a graph to be weighted, and why it matters

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

A weighted graph attaches a number to every edge, standing for cost, distance, time, or capacity. An unweighted graph treats all edges as equal, so a hop is just a hop. Adding weights changes the question from "how few edges" to "how little total cost".

That shift matters because the cheapest route is often not the shortest by edge count. Three short highway segments can beat one long back road. On a map, weights are miles or minutes; on a network, they might be latency or price.

The consequence for algorithms is real. Plain breadth-first search finds fewest-edge paths, but it ignores weights and gives wrong answers once they exist. You then need weight-aware methods like Dijkstra. So the first thing to ask about any graph problem is whether edges carry meaningful cost, because that single fact decides which algorithm is even correct.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

05

Adjacency list vs adjacency matrix - how each stores a graph

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

Two layouts dominate, and they trade memory for lookup speed. An adjacency list keeps, for each vertex, a list of its neighbors. An adjacency matrix keeps a grid of size vertices-by-vertices, where cell (i, j) marks whether an edge exists.

The cost difference is the point. A list uses space proportional to O(V + E), which is tiny when edges are few. A matrix always uses O(V^2), even for a nearly empty graph.

list:   A -> [B, C]
matrix: A row = [0, 1, 1, 0]

Speed flips the other way. Checking whether a specific edge exists is O(1) in a matrix but needs scanning a list. Most real graphs are sparse, so adjacency lists are the default choice. You reach for the matrix only when the graph is small or dense, or when you constantly test single edges.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

06

How does breadth-first search explore a graph, step by step

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

Breadth-first search fans out in layers, visiting everything one step away before anything two steps away. You keep a queue of vertices to process and a visited set so you never revisit. Start by marking the source visited and putting it in the queue.

Then repeat until the queue empties:

  • Remove the front vertex
  • Look at each of its neighbors
  • For any unvisited neighbor, mark it visited and add it to the back

Because the queue is first-in first-out, closer vertices always come out first. That is what produces the ring-by-ring order. Each vertex enters the queue once and each edge is examined once, so the cost is O(V + E).

This layered order is exactly why BFS finds the fewest-edge path in an unweighted graph. The first time you reach a vertex, you reached it by the shortest number of hops.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

07

How does depth-first search explore a graph, step by step

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

Depth-first search plunges down one path as far as it can before backing up. You pick a start, mark it visited, then move to an unvisited neighbor and repeat. When a vertex has no unvisited neighbors left, you backtrack to the previous one and try its other branches.

You can drive this with explicit recursion or with a stack.

function dfs(v, seen) {
  seen.add(v);
  for (const n of neighbors(v))
    if (!seen.has(n)) dfs(n, seen); // recurse deeper
}

The visited set stops you from looping forever on cycles. Every vertex and edge is touched once, so DFS also runs in O(V + E) time.

The deep-first order is what makes DFS natural for whole-graph questions: detecting cycles, finding connected components, and producing a topological order. It reaches the far corners of a structure quickly, which suits problems about global shape rather than nearest distance.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

08

When should you use BFS versus DFS on a graph

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

Match the traversal to what you actually want to learn. Both visit every reachable vertex in O(V + E) time, so the choice is about order and memory, not raw speed. BFS explores by distance; DFS explores by depth.

Reach for BFS when nearness matters. It finds the fewest-edge path, works well for "closest match" and level-by-level problems, and suits shallow, wide searches. Reach for DFS when structure matters. It fits cycle detection, connected components, and topological sort, where you want to fully explore each branch.

Memory is the other tiebreaker.

  • BFS holds a whole frontier, which can be huge on wide graphs
  • DFS holds only the current path, but recursion can overflow on very deep graphs

So a rough rule: if you care about shortest hops or the answer is likely nearby, use BFS. If you care about the graph's overall shape, use DFS.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

09

How do you find the shortest path in an unweighted graph

Beginner ·

Answer it yourself first - out loud, or typed below.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

Plain breadth-first search solves this directly, with no fancier algorithm needed. Because BFS reaches vertices in order of hop count, the first time it touches a vertex is along a fewest-edge path. Every edge counts as one step, so fewest edges means shortest.

To recover the actual route, record where you came from. When you first visit a neighbor, store the vertex you arrived from as its parent. Once you reach the target, walk parent pointers backward to rebuild the path, then reverse it.

parent[neighbor] = current; // set on first visit

The whole thing runs in O(V + E) time and space.

The one catch is that this only holds when every edge has the same cost. Add real weights and fewest edges no longer means cheapest, so BFS gives wrong distances. For equal-cost graphs, though, nothing beats it for simplicity.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

Related concept

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

10

What is a path, a cycle, and a connected graph

Part of Pro
11

What is a topological sort, and when do you need one

Part of Pro
12

How do you detect a cycle in a graph

Part of Pro
13

What are connected components, and how do you find them

Part of Pro
14

What do BFS and DFS cost in time and space, and why

Part of Pro
Intermediate 22
15

When is an adjacency matrix worth its memory cost over a list

Part of Pro
16

Which representation makes checking if an edge exists fast, and why

Part of Pro
17

How do BFS and DFS differ in memory on wide versus deep graphs

Part of Pro
18

Recursive vs iterative DFS - why the recursive version can crash

Part of Pro
19

Why does BFS stop working once edges carry weights

Part of Pro
20

How do you choose between BFS, Dijkstra, and Bellman-Ford

Part of Pro
21

Why does Dijkstra's algorithm break with negative edge weights

Part of Pro
22

When do you need Bellman-Ford instead of Dijkstra

Part of Pro
23

Kahn's algorithm vs DFS-based topological sort - how do they differ

Part of Pro
24

How does topological sort reveal whether a graph has a cycle

Part of Pro
25

Why does cycle detection differ between directed and undirected graphs

Part of Pro
26

Why must you track visited nodes, and what breaks without it

Part of Pro
27

When is union-find a better fit than DFS for components

Part of Pro
28

What are strongly connected components in a directed graph

Part of Pro
29

How do you check whether a graph is bipartite

Part of Pro
30

How do you model a grid or maze as a graph

Part of Pro
31

How do you find shortest paths on a weighted DAG efficiently

Part of Pro
32

How does multi-source BFS work, and when do you use it

Part of Pro
33

How do you model task dependencies as a graph to schedule them

Part of Pro
34

How would you model a social network as a graph

Part of Pro
35

How do you handle disconnected graphs during a traversal

Part of Pro
36

How do self-loops and parallel edges affect your algorithms

Part of Pro
Expert 24
37

How do you traverse a graph too large for memory

Part of Pro
38

What is compressed sparse row, and why does it beat adjacency lists

Part of Pro
39

How does pointer chasing in adjacency lists hurt cache performance

Part of Pro
40

How would you parallelize BFS, and what makes it hard

Part of Pro
41

How do you make graph traversal safe under concurrent updates

Part of Pro
42

Binary heap vs Fibonacci heap in Dijkstra - does it matter

Part of Pro
43

How does A-star search speed up shortest path, and when does it fail

Part of Pro
44

When does bidirectional search help, and what does it require

Part of Pro
45

How does Bellman-Ford detect a negative-weight cycle in a graph

Part of Pro
46

When is Floyd-Warshall the right all-pairs shortest-path choice

Part of Pro
47

How do path compression and union by rank speed up union-find

Part of Pro
48

How does Kruskal's algorithm use union-find to build an MST

Part of Pro
49

How does Prim's algorithm differ from Kruskal's, and when prefer each

Part of Pro
50

How do you track visited state on a huge implicit graph

Part of Pro
51

How does 0-1 BFS beat Dijkstra on 0-or-1 weighted graphs

Part of Pro
52

How do you guard against distance overflow in shortest-path code

Part of Pro
53

How do you maintain connectivity as edges are added over time

Part of Pro
54

What breaks in your graph code at a billion vertices and edges

Part of Pro
55

How do you find articulation points and bridges in a graph

Part of Pro
56

How do you enumerate the k shortest paths between two nodes

Part of Pro
57

How do you find shortest paths when edge weights keep changing

Part of Pro
58

How would you stream a topological order without loading everything

Part of Pro
59

How do you choose between a visited array and a hash set

Part of Pro
60

How do you test and debug a graph algorithm on tricky inputs

Part of Pro

No matches

Try a different filter or search term.

Know someone prepping for Graphs? Send them this set.
Pro · $10/mo

51 of 60 Graphs answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime

Change topic

Pick a different technology or stack. Your current topic stays put until you choose a new one.

Technologies
No technologies match “”.
Cross-cutting topics
No topics match “”.
By role
Stacks & frameworks

MEAN

MongoDB, Express, Angular, Node.js

MERN

MongoDB, Express, React, Node.js

LAMP

Linux, Apache, MySQL, PHP

Django

Python Full-Stack Development

Ruby on Rails

Convention over Configuration

Serverless on AWS

Serverless Architecture on AWS

Flutter Mobile

Flutter Cross-Platform Mobile Development

Spring Boot

Enterprise Java Development

.NET

Microsoft Ecosystem

Vue

Vue.js, Vite, TypeScript, Tailwind, Node.js

Go Backend

Golang, gRPC, PostgreSQL, Redis, RabbitMQ

FastAPI

Python, FastAPI, SQLAlchemy, PostgreSQL

React Native

React, TypeScript, Redux, Firebase

iOS Native

Swift, SwiftUI, UIKit, Firebase

Android Native

Java, Jetpack Compose, Firebase

DevOps / Platform

Docker, Kubernetes, Terraform, CI/CD

AI Engineer

LLMs, RAG, Agents, Evals

AI-Powered Developer

Claude Code, Copilot, Agentic Workflows

Core SWE Interview Prep

Data structures, algorithms, OS, concurrency, networking, git