The Flood Fill interview question simulates the "bucket fill" tool in image editors. You are given a grid, a starting pixel , and a newColor. You need to change the color of the starting pixel and all adjacent pixels (up, down, left, right) that share the same initial color. This process continues recursively for the newly colored pixels.
This is a standard question at Apple, Meta, and Microsoft. It tests your knowledge of Graph Traversal algorithms—specifically Breadth-First Search (BFS) or Depth-First Search (DFS). It evaluations whether you can handle connectivity in a grid and correctly manage a "visited" state to avoid infinite loops. It's a core Matrix interview pattern.
This problem is typically solved using DFS or BFS.
newColor, return the image immediately (to prevent infinite recursion).newColor.Grid:
1 1 1
1 1 0
1 0 1
Start at (1, 1), Color 1, New Color 2.
if (oldColor == newColor) which causes the code to keep "filling" pixels that are already the correct color.Master both BFS and DFS for grid traversals. DFS is often shorter to write recursively, but BFS is safer for very large grids where stack depth might be an issue. Mentioning this trade-off shows seniority in Graph interview patterns.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Island Perimeter | Easy | Solve | |
| Shortest Bridge | Medium | Solve | |
| Pacific Atlantic Water Flow | Medium | Solve | |
| The Maze | Medium | Solve | |
| Minesweeper | Medium | Solve |