Flood Fill
Updated
Problem statement
You are given an integer matrix image, a starting cell (sr, sc), and a replacement value color.
Starting from (sr, sc), replace the value of every cell in the same four-directionally connected component that has the starting cell's original value. Cells are adjacent only when they share an edge.
Return the resulting matrix.
Function
floodFillImage(image: int[][], sr: int, sc: int, color: int) → int[][]Examples
Example 1
image = [[1,1,1],[1,1,0],[1,0,1]]sr = 1sc = 1color = 2return = [[2,2,2],[2,2,0],[2,0,1]]Moved or new
Every 1 connected to (1, 1) through shared edges is recolored. The bottom-right 1 is connected only diagonally, so it remains unchanged.
Example 2
image = [[0,0,0],[0,0,0]]sr = 0sc = 0color = 0return = [[0,0,0],[0,0,0]]The replacement value already equals the component's value, so the matrix does not change.
Example 3
image = [[3,3,4],[3,4,4]]sr = 0sc = 2color = 7return = [[3,3,7],[3,7,7]]Moved or new
The three edge-connected cells containing 4 are recolored to 7.
Constraints
1 <= image.length, image[r].length <= 50.- All rows have the same length.
0 <= image[r][c], color <= 65535.0 <= sr < image.lengthand0 <= sc < image[0].length.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Number of IslandsFleetx · ONSITE INTERVIEW
- Number of IslandsIntuit · ONSITE INTERVIEW
- Island Count and Maximum AreaOracle · ONSITE INTERVIEW
- Maximum Number of Fish in a GridGlobalization Partners · ONSITE INTERVIEW
- Max Area of IslandGoldman Sachs · ONSITE INTERVIEW
- Max Area of IslandRoku · ONSITE INTERVIEW
- Number of IslandsWhatNot · PHONE SCREEN · ONSITE INTERVIEW
- Number of IslandsExpedia · ONSITE INTERVIEW
- Max Area of IslandBloomberg LP · ONSITE INTERVIEW
- Maximum Area of IslandSambaNova Systems · ONSITE INTERVIEW