PracHub
QuestionsCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Apple

Implement flood fill using BFS and DFS

Last updated: Mar 29, 2026

Quick Overview

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.

  • easy
  • Apple
  • Coding & Algorithms
  • Software Engineer

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.

Related Interview Questions

  • Minimum Cells to Bridge a Magic Grid - Apple (hard)
  • Find Common Prefix Across Strings - Apple (easy)
  • Find Minimum Processing Rate - Apple
  • Compute Earliest Bus Arrival - Apple (medium)
  • Find the Extra Edge - Apple (hard)
|Home/Coding & Algorithms/Apple

Implement flood fill using BFS and DFS

Apple logo
Apple
Jan 22, 2026, 12:00 AM
easySoftware EngineerTechnical ScreenCoding & Algorithms
11
0
Loading...

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]]

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Apple•More Software Engineer•Apple Software Engineer•Apple Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.