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
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

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
Grids
DFS
BFS
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-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 Problems
Sample 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...


Submit Your Answer
Markdown supported