Skip to content
Katabench
Try free

Flood Fill

Easy About 10 min Free, no account needed

The paint-bucket tool - including the click that changes nothing.

The brief

Given an image as a grid of color values, a start pixel (sr, sc) and a color, recolor the start pixel's 4-connected region of its original color and return the image.

The hidden trap

When the new color equals the region's current color, there is nothing to do, but a fill that re-visits already-painted pixels recurses forever on exactly that case. The hidden test clicks a region with its own color.

Starter code

C#
using System;

public class Solution
{
    // TODO: return the image with the 4-connected region around (sr, sc) recolored.
    public int[][] FloodFill(int[][] image, int sr, int sc, int color)
    {
        throw new NotImplementedException();
    }
}

How it is graded

Visible examples help you iterate. Hidden tests then cover edge cases and larger inputs, and every test reports server-measured time and allocated bytes.

C# coding challenges →

What it practices

Depth-first search
Recurse or use an explicit stack to explore connected regions, paths and components.

Related katas