~/problems
Problems
Basics first, then the classics, then company-style assessments. Every problem has tests you run right here; multi-level ones unlock as you go. See the roadmap.
DFS and connected components
Iterative DFS / flood fill; count and label components.
Notes
Recognise it when: you're counting islands or regions, checking connectivity, flood filling, or cloning a graph.
def count_islands(grid):
seen = set(); count = 0
for r in range(R):
for c in range(C):
if grid[r][c] == "1" and (r, c) not in seen:
count += 1
stack = [(r, c)]; seen.add((r, c))
while stack:
y, x = stack.pop()
for ny, nx in ((y+1,x),(y-1,x),(y,x+1),(y,x-1)):
if 0 <= ny < R and 0 <= nx < C and grid[ny][nx] == "1" and (ny, nx) not in seen:
seen.add((ny, nx)); stack.append((ny, nx))
return count
Gotchas: recursive DFS hits Python's recursion limit (about 1000) on big grids, so use an explicit stack. Mark cells seen when you push them.
17 problems
Interview roadmap
Graphs Grids and networks: BFS, DFS, cycles, ordering.
DFS and connected components guide
- Basics: everything reachable from a node (iterative DFS) basics py · c++ · java easy
- Storm outage: neighbourhoods without a generator py · c++ · java easy
- Number of Islands py · c++ · java easy
- Max Area of Island Snapchat py · c++ · java easy
- Clone Graph medium
- Pacific Atlantic Water Flow py · c++ · java medium
- Connect all cities with fewest roads py · c++ · java easy
- Count and map a machine tree by messages 3 levels OpenAI hard
- Number of Connected Components in an Undirected Graph py · c++ · java easy
- Minimum servers to activate Airbnb py · c++ · java hard
- Use every boarding pass exactly once Citadel py · c++ · java hard
- Cutting a branch out of a forest Pinterest py · c++ · java easy
- Surrounded Regions py · c++ · java medium
- Chain every word end to start py · c++ · java hard
- Evaluate Division py · c++ · java medium
- Island Perimeter py · c++ · java easy
- Find the Town Judge py · c++ · java easy