Welcome to Day 10 of your coding interview journey.
So far, youβve built strong fundamentals:
- β Arrays, HashMaps, Sliding Window
- β Two Pointers, Intervals, Binary Search
- β Trees & Binary Search Trees
Today, we step into a game-changing topic:
π Graphs
π§ What is a Graph?
A Graph is a collection of:
- Nodes (vertices)
- Edges (connections between nodes)
Graphs can represent:
- Social networks
- Maps & navigation
- Networks & systems
π Two Core Traversal Techniques
To solve graph problems, you must master:
1. DFS β Depth-First Search
- Go deep first, then backtrack
- Usually implemented with recursion
2. BFS β Breadth-First Search
- Explore level by level
- Uses a queue
π§© Problem β Number of Islands
π§ Problem
You are given a 2D grid of '1' (land) and '0' (water):
π Count how many islands exist.
An island is formed by connecting adjacent lands (horizontally or vertically).
β Example
grid = [
["1","1","0","0"],
["1","1","0","0"],
["0","0","1","0"],
["0","0","0","1"]
]
Output:
3
π‘ Approach 1 β DFS (Recursive)
π§ Key Idea
π Every time you find land ('1'):
- Thatβs a new island
- Use DFS to mark the entire island as visited
π οΈ Solution in Python (DFS)
def num_islands(grid):
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
count = 0
def dfs(r, c):
if (
r < 0 or c < 0 or
r >= rows or c >= cols or
grid[r][c] == "0"
):
return
grid[r][c] = "0" # mark as visited
dfs(r + 1, c)
dfs(r - 1, c)
dfs(r, c + 1)
dfs(r, c - 1)
for r in range(rows):
for c in range(cols):
if grid[r][c] == "1":
count += 1
dfs(r, c)
return count
β‘ Complexity (DFS)
- Time:
O(m * n) - Space:
O(m * n)(recursion stack)
π‘ Approach 2 β BFS (Queue)
π§ Key Idea
Same logic, but instead of recursion:
π Use a queue to explore neighbors level by level
π οΈ Solution in Python (BFS)
from collections import deque
def num_islands_bfs(grid):
if not grid:
return 0
rows, cols = len(grid), len(grid[0])
count = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == "1":
count += 1
queue = deque([(r, c)])
while queue:
x, y = queue.popleft()
if (
x < 0 or y < 0 or
x >= rows or y >= cols or
grid[x][y] == "0"
):
continue
grid[x][y] = "0"
queue.append((x + 1, y))
queue.append((x - 1, y))
queue.append((x, y + 1))
queue.append((x, y - 1))
return count
π― DFS vs BFS
| Feature | DFS | BFS |
|---|---|---|
| Strategy | Go deep | Level by level |
| Data structure | Recursion / Stack | Queue |
| Use case | Exploration | Shortest path |
β οΈ Common Mistakes
- β Not marking nodes as visited
- β Going out of bounds
- β Counting the same island multiple times
- β Modifying input without intention
π§ͺ Practice Problems
To master graphs, try:
- Clone Graph
- Pacific Atlantic water flow
- Course schedule
- Rotten oranges
- Word ladder
π― Key Takeaways
- Graph problems = traversal problems
- Master:
- DFS (recursion mindset)
- BFS (queue mindset)
- Think in terms of:
- Nodes
- Neighbors
- Visited states
π Your Challenge (Day 10)
Solve this:
Given a grid, find the size of the largest island.
π Final Thoughts
Graphs are one of the most important topics in coding interviews.
Once you master DFS and BFS, you unlock problems that many candidates struggle with.