Flood Fill
Updated
Problem statement
A paint program stores an image as an m x n grid of integers image, where image[i][j] is the color of the pixel in row i and column j. You are given a start pixel (sr, sc) and a new color color.
Implement the program's fill tool. It changes the start pixel to color, and it also changes every pixel that has the start pixel's original color and can be reached from the start pixel by steps up, down, left or right through pixels of that same color. Two pixels that touch only at a corner are not connected.
Examples
Example 1
image = [[0,0,1],[0,1,1],[2,0,1]]sr = 0sc = 0color = 5return = [[5,5,1],[5,1,1],[2,0,1]]The start pixel (0, 0) has color 0. The pixels (0, 1) and (1, 0) also have color 0 and share an edge with it, so all three become 5. The 0 at (2, 1) touches that area only at a corner, so it keeps its color.
Unlock this recently reported problem
FastPrep Pro gives you full access to interview problems reported within the last week.
- Full problem statement and constraints
- 2 more worked examples, explained
- Guided hints and editorial
- Run your code on real test cases
$99 billed yearly — or $19 month-to-month. Cancel anytime.
More Amazon Onsite Interview questions
- Next Greater Element IONSITE INTERVIEW · Seen Dec 2026
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Optimal Account BalancingONSITE INTERVIEW · Seen Dec 2026
- Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Merge k Sorted ListsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Assign Escalations to TeamsONSITE INTERVIEW · Seen Nov 2026
- Best Time to Buy and Sell StockONSITE INTERVIEW · Seen Nov 2026
- Maximum Earnings Across WarehousesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Flood FillGoogle · ONSITE INTERVIEWGoogle interview questions
- Number of IslandsFleetx · ONSITE INTERVIEW
- Number of IslandsIntuit · ONSITE INTERVIEWIntuit interview questions
- Island Count and Maximum AreaOracle · ONSITE INTERVIEWOracle interview questions
- Maximum Number of Fish in a GridGlobalization Partners · ONSITE INTERVIEW
- Max Area of IslandGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Max Area of IslandRoku · ONSITE INTERVIEWRoku interview questions
- Number of IslandsWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
- Number of IslandsExpedia · ONSITE INTERVIEWExpedia interview questions
- Max Area of IslandBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
Amazon interview experiences
- Amazon Experienced SDE Onsite Questions: 1 Experience
- Amazon SDE Onsite Interview Questions: 9 Experiences
- Amazon SDE II and L5 FDE Interview Questions: 4 Experiences
- Amazon Full-Time OA Problems, Earlier Reports (Part 5 of 5)
- Amazon Full-Time OA Problems, Recent Reports (Part 4 of 5)
- Amazon New Grad OA Problems: Every Task We Have (Part 3 of 5)