PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|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

  • Compute Earliest Bus Arrival - Apple (medium)
  • Find the Extra Edge - Apple (hard)
  • Rotate a Matrix In Place - Apple (medium)
  • Encode and Rebuild a Binary Tree - Apple (hard)
  • Wrap Matching Substrings in Bold Tags - Apple (medium)
Apple logo
Apple
Jan 22, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
7
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]]

Comments (0)

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 7,500+ 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
  • Compare Platforms
  • Discord Community

Support

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

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.