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
PROBLEM-SOLVING TECHNIQUES

Breadth-First Search

Explore level by level with a queue - shortest paths when every edge costs the same.

Intermediate9 problems2 interactive subtopicsO(V + E) time · O(V) space
Practice in AI DSA
WalkthroughOverviewRecognize itMistakes & edge casesKnowledge check (3)Practice
Algorithms & custom examples (2)Open in full playground →
STEP 1/5Start at A
dist[A] = 0
A
B
C
D
E
0
QUEUE, FRONT FIRST
A
  • waiting / queued
  • active this step
  • current
  • visited / done
  • result
Space Play←→ StepR Replay
1 / 5
PYTHONA: B, C · B: A, D · C: A, D · D: B, C, E · E: D - from A to E
1from collections import deque
2
3def shortest_path(graph, start, goal):
4 dist = {start: 0}
▶5 queue = deque([start])
6 while queue:
7 node = queue.popleft()
8 if node == goal:
9 return dist[node]
10 for nxt in graph[node]:
11 if nxt not in dist:
12 dist[nxt] = dist[node] + 1
13 queue.append(nxt)
14 return -1
STEP 1 OF 5Setup

Start at A

A is 0 steps from itself and is the only node in the queue.

VARIABLES

node
-
queue
[A]
dist
{A: 0}
02

Intuition & complexity

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

What it is

BFS visits the start, then all its neighbours, then theirs, using a queue. The first time it reaches a node is along a shortest path in an unweighted graph.

Why it matters

Shortest-path and minimum-steps questions on unweighted graphs and grids are BFS by default.

Think of it like this

Ripples in a pond: the circle reaches nearer points before farther ones.

Use it when

  • Minimum number of steps or moves
  • Level order traversal
  • Things spreading each minute - rotting oranges, fire

Avoid it when

  • Edges with different costs - use Dijkstra
  • You need to follow one path deeply - DFS is lighter

Complexity cheatsheet

OperationTimeSpace
BFS on a graphO(V + E)O(V)
BFS on an r × c gridO(r × c)O(r × c)
Each enqueue or dequeueO(1)-

Often confused with: DFS - it reaches every node too, but not along shortest paths.

03

How to recognize it

Clues in interview prompts that point straight to this pattern.

shortest pathminimum stepslevelnearestspreads each minute
  • Mark nodes visited when you enqueue them, not when you dequeue them
  • Multi-source BFS: put every start in the queue first
04

Mistakes & edge cases

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

Common mistakes

  • Marking nodes visited on dequeue - the same node gets queued many times
  • Using a list with pop(0) as the queue
  • Forgetting grid bounds or obstacle checks

Edge cases to test

  • Start equals goal - distance 0
  • Goal unreachable - return -1
  • A disconnected graph
Interview tip: Say why BFS gives shortest paths: nodes leave the queue in order of distance.
05

Knowledge check

Quick check to lock in the intuition.

Question 1 of 3

Edges now have different weights (1, 5, 2…). Is BFS still correct for shortest paths?

06

Practice

Apply what you just watched on real interview problems.

Generate Breadth-First Search problem in AI DSA Practice →
  • BeginnerFlood FillLeetCode 733 ↗ (opens in a new tab)
  • IntermediateRotting OrangesLeetCode 994 ↗ (opens in a new tab)
  • AdvancedWord LadderLeetCode 127 ↗ (opens in a new tab)
Dynamic ProgrammingDepth-First Search