Repaint the connected region of a picture

Graphs Junior Python 3.13+ Reviewed By Best Latest Apps

Pick the fix

The start pixel's colour already equals the new colour. What should a version that uses colour as the visited mark do?

Options

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A Return straight away, because nothing would change

Right: without this check it keeps revisiting the same pixels.

When the old and new colours match, a repainted pixel still looks unvisited, so the early exit is required.

Why not the others?
  • B Repaint the start pixel and continue. Every neighbour still matches, so the walk never ends.
  • C Use a different new colour. The caller chose the colour; the function should handle this case.

30-second answer

This is flood fill. Remember the starting colour; if it already equals the new colour, return a copy unchanged. Otherwise walk from the start with a stack, repainting each matching pixel and pushing its neighbours. Each pixel is repainted once: O(R·C) time and space (the copy).

Practise saying it · 30 s. No recording, just a timer

Explain flood fill on a picture.

Say your answer out loud, then check which of these points you covered:

  • Remember the old colour
  • Stack walk, repaint on push
  • Same-colour early exit

Deep dive

The early return when the colours match prevents an infinite loop in versions that use the colour itself as the 'visited' mark.

Repainting as you push doubles as the visited set.

Paint programs use this with scanline tricks to process whole runs at a time.

Python solution Ran on Python 3.13.5 · mypy --strict
Python 3.13
STEPS = ((1, 0), (-1, 0), (0, 1), (0, -1))


def repaint(picture: list[list[int]], r: int, c: int,
            colour: int) -> list[list[int]]:
    out = [row[:] for row in picture]
    old = out[r][c]
    if old == colour:
        return out
    out[r][c] = colour
    todo = [(r, c)]
    while todo:
        y, x = todo.pop()
        for dy, dx in STEPS:
            ny, nx = y + dy, x + dx
            if (0 <= ny < len(out) and 0 <= nx < len(out[0])
                    and out[ny][nx] == old):
                out[ny][nx] = colour
                todo.append((ny, nx))
    return out

Common mistakes

  • Forgetting the same-colour case and looping forever.
  • Repainting pixels that only touch diagonally.

Senior take & follow-ups

Mention the same-colour edge case before you start; interviewers often test it.

Follow-up questions

Think of your answer first, then open the hint.

  1. How would you fill only the border of the region?

    Show hint

    Repaint pixels that have at least one neighbour outside the region.

  2. What if the picture is huge?

    Show hint

    Use an explicit stack or a scanline fill to avoid deep recursion.

From the author

DSA Master - Data & Algorithms

Free on iPhone and iPad: watch 50+ data structures and algorithms step through animations, run the code in the playground, and try the daily challenge.

Suggest a fix if something here is wrong or out of date.