Recolor a Connected Region in a Sticker Image With Flood Fill
Last updated: November 29, 2026
Quick Overview
A sticker editor stores an image as a grid of color values. Given a starting pixel and a new color, recolor that pixel and every pixel connected to it in four directions that shares its original color.
Meta
November 29, 20260
0
0 solved
A sticker editor stores an image as a grid of color values. Given a starting pixel and a new color, recolor that pixel and every pixel connected to it in four directions that shares its original color.
Flood fill is a common Meta phone-screen opener, here framed around a paint bucket tool in a Messenger or Instagram sticker editor. The interviewer checks a clean grid traversal, the early exit when the new color equals the old one, and awareness of recursion depth on large images.
What the Interviewer Expects
- Captures the original color before changing anything
- Returns early when the new color equals the original
- Uses an explicit stack or queue to avoid recursion limits
- Checks bounds before reading neighbors
- Mutates in place and returns the grid
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you support eight-direction connectivity?
- How would you fill with a tolerance, treating similar colors as equal?
- What is the worst-case memory of the stack and how could a scanline fill reduce it?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: grid img of ints, start (r, c), and new_color. Output: the grid after filling. Only 4-directional neighbors count. Images can be 4K, so millions of pixels; recursion would overflow....
High-Level Design
Let old be the starting pixel's color. If old == new_color, return immediately, otherwise the traversal would loop forever on pixels that look unvisited. Push the start onto a stack and recolor pi...