Recolor the Connected Component of a Pixel

Quick Overview

Recolor the Connected Component of a Pixel. Starting at image[sr][sc], recolor every pixel reachable through side-adjacent pixels of that starting pixel's original color.

Recolor the Connected Component of a Pixel

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Starting at image[sr][sc], recolor every pixel reachable through side-adjacent pixels of that starting pixel's original color. Diagonal contact alone does not connect pixels. Return the modified rectangular image. ```cpp class Solution { public: vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color); }; ``` ### Examples **Example 1:** ```text image = [[3,3],[3,4]], sr = 0, sc = 1, color = 7 Output: [[7,7],[7,4]] Explanation: The three pixels of color 3 form one side-connected component. ``` **Example 2:** ```text image = [[2,1],[1,2]], sr = 0, sc = 0, color = 5 Output: [[5,1],[1,2]] Explanation: The other 2 touches only diagonally. ``` ### Constraints - Image dimensions m,n are each from 1 through 50. - Pixel values and color lie in [0,65535]. - Starting indices satisfy 0 <= sr < m and 0 <= sc < n. ### Contract considerations Preserve the original color for membership tests and return the recolored image. ```hint Read the boundary condition When can the new color also serve as a visited marker? ``` ### Boundary reminders Preserve the original color for membership tests and return the recolored image.

Overview: Recolor the Connected Component of a Pixel. Starting at image[sr][sc], recolor every pixel reachable through side-adjacent pixels of that starting pixel's original color.

Amazon logo
Amazon
Source window: 0–3 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Starting at image[sr][sc], recolor every pixel reachable through side-adjacent pixels of that starting pixel's original color. Diagonal contact alone does not connect pixels. Return the modified rectangular image.

class Solution {
public:
    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color);
};

Examples

Example 1:

image = [[3,3],[3,4]], sr = 0, sc = 1, color = 7
Output: [[7,7],[7,4]]
Explanation: The three pixels of color 3 form one side-connected component.

Example 2:

image = [[2,1],[1,2]], sr = 0, sc = 0, color = 5
Output: [[5,1],[1,2]]
Explanation: The other 2 touches only diagonally.

Constraints

  • Image dimensions m,n are each from 1 through 50.
  • Pixel values and color lie in [0,65535].
  • Starting indices satisfy 0 <= sr < m and 0 <= sc < n.

Contract considerations

Preserve the original color for membership tests and return the recolored image.

Boundary reminders

Preserve the original color for membership tests and return the recolored image.