Recolor a Connected Region in a Drawing Canvas With Flood Fill

Last updated: November 29, 2026

Quick Overview

Given a 2D grid of pixel colors, a starting pixel, and a new color, recolor the starting pixel and every pixel 4-directionally connected to it that shares its original color. Return the updated grid.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Given a 2D grid of pixel colors, a starting pixel, and a new color, recolor the starting pixel and every pixel 4-directionally connected to it that shares its original color. Return the updated grid.

A warm-up in the style of a Google phone screen, framed around the paint bucket tool in Google Drawings or Slides. The interviewer checks that you can write a clean BFS or DFS on a grid, handle bounds, and avoid the infinite loop when the new color equals the old one.

What the Interviewer Expects
  • Clarify 4-directional versus 8-directional connectivity
  • Handle the case where the new color equals the original color
  • Use an explicit stack or queue to avoid recursion depth limits on large canvases
  • Check bounds before reading neighbors
  • State time and space in terms of the region size
Key Topics to Cover
flood fill
BFS
grid traversal
connected components
in-place mutation
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you support 8-directional fill?
  • What if the canvas is 20000 by 20000 and does not fit in memory?
  • How would you fill with a tolerance, treating similar colors as equal?
  • How would you make the fill undoable?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: grid (list of lists of ints), start (sr, sc), new_color.
  • Recolor the 4-connected region that shares grid[sr][sc]'s color.
  • Mutate in place and return the grid.
  • Grid can be large ...
High-Level Design

The region is a connected component in an implicit graph where each pixel is a node and edges join equal-colored orthogonal neighbors. Any traversal that visits the whole component works. I use an ite...


Submit Your Answer
Markdown supported