Quick 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.

Fill the connected color region containing a selected pixel

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Onsite

A drawing is represented by a rectangular integer-color grid. Starting from a selected pixel, replace its entire connected region of the original color with `new_color`. For this version, connectivity uses the four horizontal and vertical neighbors. ### Function Signature ```python def fill_region(image: list[list[int]], start_row: int, start_col: int, new_color: int) -> list[list[int]]: ``` ### Rules Return the complete updated grid. Only pixels reachable from the start through pixels of its original color are changed. Diagonal contact alone does not connect pixels. The implementation may return a copy. ### 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 ```text Input: image = [[1,1,0],[1,0,1]], start_row = 0, start_col = 0, new_color = 2 Output: [[2,2,0],[2,0,1]] ``` The lower-right pixel is not connected to the selected region. ```text Input: image = [[3]], start_row = 0, start_col = 0, new_color = 3 Output: [[3]] ``` Replacing a color with itself leaves the image unchanged.

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

A drawing is represented by a rectangular integer-color grid. Starting from a selected pixel, replace its entire connected region of the original color with `new_color`. For this version, connectivity uses the four horizontal and vertical neighbors. ### Function Signature ```python def fill_region(image: list[list[int]], start_row: int, start_col: int, new_color: int) -> list[list[int]]: ``` ### Rules Return the complete updated grid. Only pixels reachable from the start through pixels of its original color are changed. Diagonal contact alone does not connect pixels. The implementation may return a copy. ### 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 ```text Input: image = [[1,1,0],[1,0,1]], start_row = 0, start_col = 0, new_color = 2 Output: [[2,2,0],[2,0,1]] ``` The lower-right pixel is not connected to the selected region. ```text Input: image = [[3]], start_row = 0, start_col = 0, new_color = 3 Output: [[3]] ``` Replacing a color with itself leaves the image unchanged.

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

  1. Reachability follows four directions and only through the selected pixel's original color.
  2. What happens when the original and replacement colors match?

Fill the connected color region containing a selected pixel

hardAmazonSoftware Engineer

A drawing is represented by a rectangular integer-color grid. Starting from a selected pixel, replace its entire connected region of the original color with new_color. For this version, connectivity uses the four horizontal and vertical neighbors.

Function Signature

def fill_region(image: list[list[int]], start_row: int, start_col: int, new_color: int) -> list[list[int]]:

Rules

Return the complete updated grid. Only pixels reachable from the start through pixels of its original color are changed. Diagonal contact alone does not connect pixels. The implementation may return a copy.

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: image = [[1,1,0],[1,0,1]], start_row = 0, start_col = 0, new_color = 2
Output: [[2,2,0],[2,0,1]]

The lower-right pixel is not connected to the selected region.

Input: image = [[3]], start_row = 0, start_col = 0, new_color = 3
Output: [[3]]

Replacing a color with itself leaves the image unchanged.

Examples

Example 1
Input
([[1, 1, 0], [1, 0, 1]], 0, 0, 2)
Output
[[2, 2, 0], [2, 0, 1]]
Notes
Leave the separated lower-right pixel unchanged.
Example 2
Input
([[3]], 0, 0, 3)
Output
[[3]]
Notes
A replacement equal to the original color is a no-op.

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.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def fill_region(image: list[list[int]], start_row: int, start_col: int, new_color: int) -> list[list[int]]:
Sample call
fill_region([[1, 1, 0], [1, 0, 1]], 0, 0, 2)
Language
Case 1
Input
([[1, 1, 0], [1, 0, 1]], 0, 0, 2)
Expected
[[2, 2, 0], [2, 0, 1]]
Case 2
Input
([[3]], 0, 0, 3)
Expected
[[3]]

Browse More Questions

Show the approach

Approach

Read the selected color and return immediately if it equals the replacement. Otherwise recolor the selected cell and start a traversal from it. Whenever a horizontal or vertical neighbor still has the original color, recolor it before adding it to the traversal queue.

Every recolored cell has a path of original-color cells back to the start, because each was reached from a previous cell on such a path. Conversely, following an original-color path makes every successive cell eligible, so the traversal reaches the entire connected region. Recoloring on discovery avoids re-enqueuing a cell.

Time complexity:
O(rows × columns) worst case
Space complexity:
O(rows × columns) worst case