Fill the connected color region containing a selected pixel
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
Overview: Implement a drawing fill operation that recolors the selected pixel’s connected region. Preserve disconnected matching pixels, define four-neighbor adjacency, and handle unchanged colors and large regions safely.
Read the full Amazon Software Engineer interview experience this question came from
Constraints
- The grid has between 1 and 300 rows and between 1 and 300 columns, all rows equal in length.
- All colors, including `new_color`, are integers between 0 and 1000000.
- The start row and column identify a valid cell.
Examples
Input: ([[1, 1, 0], [1, 0, 1]], 0, 0, 2)
Expected Output: [[2, 2, 0], [2, 0, 1]]
Explanation: Leave the separated lower-right pixel unchanged.
Input: ([[3]], 0, 0, 3)
Expected Output: [[3]]
Explanation: A replacement equal to the original color is a no-op.
Hints
- Reachability follows four directions and only through the selected pixel's original color.
- What happens when the original and replacement colors match?