~/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.

Graphs

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

esc