~/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.
BFS / multi-source BFS
Level-by-level search; seed the queue with every source.
Main path: 8 problems, about 3 hours.
Notes
Recognise it when: shortest path in steps on an unweighted graph or grid, "minimum minutes/moves", spreading, level-by-level processing.
q = deque(sources) # multi-source: seed ALL starts
seen = set(sources)
steps = 0
while q:
for _ in range(len(q)): # one level
node = q.popleft()
for nxt in neighbours(node):
if nxt not in seen:
seen.add(nxt)
q.append(nxt)
steps += 1
Gotchas
- Mark nodes seen when you enqueue them, not when you dequeue them, or you get duplicates.
- For simultaneous updates (cellular automata), BFS levels give you that for free. Never re-scan the whole grid each step.
16 problems
Interview roadmap
Graphs Grids and networks: BFS, DFS, cycles, ordering.
BFS / multi-source BFS guide
- Play first: The Maze 1-minute game Breadth-first search
- Shortest hop counts from one node basics py · c++ · java easy
- Museum: fewest doors to an exhibit code the boss py · c++ · java easy
- Shortest Path in Binary Matrix py · c++ · java medium
- Rotting Oranges ▶ boss game py · c++ · java medium
- Distance to nearest zero py · c++ · java medium
- Walls and Gates py · c++ · java medium
- Open the Lock py · c++ · java medium
- Word Ladder py · c++ · java hard
More practice: 8 problems
- Score a tile-laying board Airbnb py · c++ · java easy
- Tree nodes whose distances form a Pythagorean triple py · c++ · java easy
- Friend suggestions by mutual friends CitadelOracle py · c++ · java easy
- Count Objects in Pin Pinterest py · c++ · java medium
- Bus Routes GooglePinterest py · c++ · java hard
- Fastest commute on a mode grid assessment 3 levels Databricks py · c++ · java hard
- Office floor: bathrooms and cakes assessment 3 levels Snowflake py · c++ · java hard
- Infection spread (multi-part simulation) assessment 4 levels OpenAI py · c++ · java hard