~/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.
Main path: 8 problems, about 3 hours.
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.
18 problems
Interview roadmap
Graphs Grids and networks: BFS, DFS, cycles, ordering.
DFS and connected components guide
- Play first: The Burning Isles 1-minute game Flood fill (DFS or BFS on a grid)
- Everything reachable from a node basics py · c++ · java easy
- Storm outage: neighbourhoods without a generator py · c++ · java easy
- Island Perimeter py · c++ · java easy
- Number of Islands code the boss py · c++ · java medium
- Max Area of Island Snapchat py · c++ · java medium
- Clone Graph py · c++ · java medium
- Pacific Atlantic Water Flow py · c++ · java medium
- Use every boarding pass exactly once Citadel py · c++ · java hard
More practice: 10 problems
- Find the Town Judge py · c++ · java easy
- Connect all cities with fewest roads py · c++ · java easy
- Cutting a branch out of a forest Pinterest py · c++ · java easy
- Number of Connected Components in an Undirected Graph py · c++ · java medium
- Surrounded Regions py · c++ · java medium
- Evaluate Division py · c++ · java medium
- Minimum servers to activate Airbnb py · c++ · java hard
- Chain every word end to start py · c++ · java hard
- Critical Connections in a Network py · c++ · java hard
- Count and map a machine tree by messages assessment 3 levels OpenAI py · c++ · java hard