Graph / DFS•Pattern: DFS / BFS•Medium

Flood fill algorithm

Problem Intuition & Objective

Recolor a connected region in a 2D grid starting from a given pixel.

Time ComplexityO(m×n)
Optimal
Space ComplexityO(m×n) recursion
Memory Bounds
Optimal Algorithmic Approach

DFS flood fill with color-match condition.

Core Invariants & Edge Cases

  • DFS/BFS from the starting cell
  • Only spread to cells with the same original color
  • Guard against infinite recursion if newColor == oldColor
Previous ProblemUnion-Find — connected components with dynamic unionNext Problem Pacific Atlantic water flow

System Design & Architecture Roadmap

Coding interviews are only 50% of the bar. Master High-Level Design, distributed systems, and Object-Oriented LLD.