Interview's MateDSA Visualizer
Interview's Mate

DSA Visualizer

Learn by watching algorithms run

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
All topics
DATA STRUCTURES

Graphs

Nodes connected by edges - networks, maps, dependencies and grids.

Intermediate10 problems4 interactive subtopicsO(V + E) time · O(V + E) space
Practice in AI DSA
WalkthroughOverviewRecognize itMistakes & edge casesKnowledge check (3)Practice
Algorithms & custom examples (4)Open in full playground →
STEP 1/5Build the adjacency list
edges stored both ways
0
1
2
3
4
  • current
  • visited / done
Space Play←→ StepR Replay
1 / 5
PYTHONn = 5, edges = [[0, 1], [1, 2], [3, 4]]
1def count_components(n, edges):
2 graph = {i: [] for i in range(n)}
3 for a, b in edges:
4 graph[a].append(b)
▶5 graph[b].append(a)
6 seen, count = set(), 0
7 for start in range(n):
8 if start in seen:
9 continue
10 count += 1
11 stack = [start]
12 while stack:
13 node = stack.pop()
14 if node not in seen:
15 seen.add(node)
16 stack.extend(graph[node])
17 return count
STEP 1 OF 5Setup

Build the adjacency list

Each edge is added in both directions: 0: [1], 1: [0, 2], 2: [1], 3: [4], 4: [3].

VARIABLES

count
0
seen
set()
02

Intuition & complexity

The core mental model, trade-offs, and Big-O cost.

What it is

A graph is a set of vertices and edges. Interview graphs are usually stored as adjacency lists: each node maps to its neighbours.

Why it matters

Anything with relationships - friends, flights, prerequisites, grid cells - is a graph problem in disguise.

Think of it like this

A metro map: stations are nodes and tracks are edges.

Use it when

  • Connectivity and counting components
  • Paths between things
  • Grids where cells connect to their neighbours

Avoid it when

  • Strict hierarchies with one parent each - tree techniques are simpler
  • Dense graphs where an adjacency matrix is easier

Complexity cheatsheet

OperationTimeSpace
Build an adjacency listO(V + E)O(V + E)
BFS or DFS traversalO(V + E)O(V)
Check one edge (adjacency list)O(degree)O(1)

Often confused with: trees - graphs with no cycles and exactly one path between any two nodes.

03

How to recognize it

Clues in interview prompts that point straight to this pattern.

connectedislandsnetworkneighbourspath between
  • An r × c grid is a graph with up to 4 edges per cell
  • Always track visited nodes - graphs can have cycles
04

Mistakes & edge cases

Catch common off-by-one bugs and corner cases before submitting.

Common mistakes

  • Adding only one direction for undirected edges
  • Not tracking visited nodes - infinite loops on cycles
  • Missing isolated nodes that have no edges

Edge cases to test

  • No edges at all - every node is its own component
  • Self-loops and duplicate edges
  • A disconnected graph
Interview tip: Ask first: directed or undirected? Weighted? Can there be cycles? Each answer changes the algorithm.
05

Knowledge check

Quick check to lock in the intuition.

Question 1 of 3

Traversing a graph with V nodes and E edges using BFS or DFS costs…

06

Practice

Apply what you just watched on real interview problems.

Generate Graph Theory problem in AI DSA Practice →
  • BeginnerFind the Town JudgeLeetCode 997 ↗ (opens in a new tab)
  • IntermediateClone GraphLeetCode 133 ↗ (opens in a new tab)
  • AdvancedReconstruct ItineraryLeetCode 332 ↗ (opens in a new tab)
Heaps / Priority QueuesTries