Paint Bucket Fill for a Connected Color Region in MS Paint

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 connected to it in four directions that shares its original color, as the Paint bucket tool does.

Microsoft
Coding & Algorithms
Software Engineer
Microsoft
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 connected to it in four directions that shares its original color, as the Paint bucket tool does.

A friendly graph traversal warm-up that fits Microsoft phone screens and the Paint product. The interviewer checks that you pick BFS or DFS, avoid infinite loops when the new color equals the old one, and know when recursion depth becomes a risk on large images.

What the Interviewer Expects
  • Treat pixels as nodes with four-directional edges
  • Return early when the new color equals the original
  • Use an explicit stack or queue to avoid recursion limits
  • Check bounds before reading neighbors
  • State O(rows x cols) worst case
Key Topics to Cover
BFS
DFS
grids
flood fill
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 eight-directional fill?
  • How would you fill with a tolerance, so near-identical colors also fill?
  • How would a scanline fill reduce stack usage on 4K images?
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 image of integers, start (r, c), and new_color. Output: the grid after recoloring the connected region of the start pixel's original color. Modify in place and return it. A real Pain...

High-Level Design

Read the original color at the start. If it already equals new_color, return immediately; otherwise the traversal would never mark anything as visited and would loop forever. Then run an iterative D...


Submit Your Answer
Markdown supported