Skip to main content

Flood Fill

Perform flood fill starting at (sr, sc) Replace connected pixels of same color Example: [[1,1,1],[1,1,0],[1,0,1]], sr=1, sc=1, color=2

Given a non-empty rectangular grid of integer colors image, a valid starting cell (sr, sc), and an integer color, return the grid after recoloring the starting cell and every cell connected to it through cells of the same original color. Connections use only horizontal or vertical neighbors, never diagonals. All other cells keep their original values. If color already equals the starting color, return the unchanged grid.

Examples

You write

flood_fill(image: List[List[int]], sr: int, sc: int, color: int)

  1. Example 1

    Input
    image = [[1,1,1],[1,1,0],[1,0,1]], sr = 1, sc = 1, color = 2
    returns
    [[2,2,2],[2,2,0],[2,0,1]]
  2. Example 2

    Input
    image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, color = 0
    returns
    [[0,0,0],[0,0,0]]

The pattern behind it

Arrays & Hashing

I reach for it when

  • You catch yourself scanning the array again for every element.
  • The problem asks about duplicates, pairs, counts or groups.

Say this before you type. Say what the key is and what the value remembers, before you write the loop.

All 127 Arrays & Hashing problems

BFS & DFS

I reach for it when

  • You walk a grid, a graph or a tree and visit each cell once.
  • The word shortest appears and every step costs the same.

Say this before you type. Say why breadth or depth fits, and exactly when a cell counts as visited.

All 52 BFS & DFS problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1Flood Filleasy

Prep guides that list this problem

Our company tags come from public interview write-ups. None of these is a confirmed question.