The Coloring A Border interview question asks you to identify and recolor the "border" of a specific connected component in a grid. You are given a grid, a starting cell (row, col), and a newColor. The component consists of all cells reachable from the start that have the same original color. A cell in this component is on the "border" if it is at the edge of the grid or if it is adjacent to a cell of a different color.
Microsoft and Google use the Matrix interview pattern to evaluate your grid traversal skills. It’s a twist on the classic "Flood Fill" algorithm. While Flood Fill colors the entire area, this problem requires you to apply specific logic to determine which cells within the area should be modified. It tests your ability to maintain state (which cells have been visited) while checking neighbors for color boundaries.
This problem is best solved using Breadth-First Search (BFS) or Depth-First Search (DFS).
Grid:
1 1 1
1 1 1
1 1 1
Target: (1, 1) (center), newColor = 2.
1. The whole grid is the component.(0, 0) is a border because it's at the edge.(1, 1) (center) is NOT a border because all 4 neighbors are inside and have color 1.1s will be colored 2.When modifying a grid based on neighborhood properties, always separate the "identification" phase from the "modification" phase. This ensures that the state of the grid remains consistent throughout the traversal.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Find All Groups of Farmland | Medium | Solve | |
| Pacific Atlantic Water Flow | Medium | Solve | |
| Shortest Bridge | Medium | Solve | |
| Minesweeper | Medium | Solve | |
| The Maze | Medium | Solve |