Implement flood fill using BFS and DFS
Company: Apple
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
## Problem
You are given a 2D grid `image` of size `m × n`, where each cell contains an integer color value. You are also given a starting cell `(sr, sc)` and an integer `newColor`.
Perform a **flood fill** starting from `(sr, sc)`:
- Let `oldColor = image[sr][sc]`.
- Recolor `(sr, sc)` and every cell **4-directionally connected** (up, down, left, right) to `(sr, sc)` that has color `oldColor` to `newColor`.
- Return the modified `image`.
If `oldColor == newColor`, the grid should remain unchanged.
## Requirements
Write the solution in three ways:
1. Using **BFS**.
2. Using **recursive DFS**.
3. Using **iterative DFS** (explicit stack; no recursion).
## Inputs / Outputs
- **Input:** `image: int[m][n]`, `sr: int`, `sc: int`, `newColor: int`
- **Output:** Modified `image` after flood fill.
## Constraints (typical)
- `1 ≤ m, n ≤ 50`
- `0 ≤ sr < m`, `0 ≤ sc < n`
- Color values are non-negative integers.
## Example
Given:
- `image = [[1,1,1],[1,1,0],[1,0,1]]`, `sr = 1`, `sc = 1`, `newColor = 2`
Output:
- `[[2,2,2],[2,2,0],[2,0,1]]`
Quick Answer: This question evaluates proficiency in graph traversal and matrix manipulation, specifically the implementation of BFS and DFS variants for flood fill along with handling recursion, iterative stacks, and in-place updates.