FastPrepFlood Fill
Problems/Flood Fill

Flood Fill

Amazon logoAmazon● EasyFULLTIMEONSITE INTERVIEW

Updated Dec 2026

QuestionExamples

Problem statement

Source note: Reported in an Amazon onsite for a full-time software engineer role on the EC2 team. In the third round, after a behavioral question, the coding task was to build the fill tool of a paint program. The candidate called it a version of the classic count-the-islands grid problem, with spreading a bit like the rotting-oranges grid problem.

The report gives no function signature, example, rule for which pixels count as connected, or limits. Here the image is a grid of integer colors with a start pixel and a new color, pixels connect only through shared edges, the changed image is returned, the image stays the same when the new color equals the old one, and the limits are our own.

Our version is about 85% the same as the reported question.

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.

The problem statement continues
Pro

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]]
image
012000110112201
→
return
012055115112201
Moved or new

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.

02 · Example 2
FastPrep Pro
Reported in 1 Amazon interview this week

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
$8.25/month

$99 billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week

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)
See Amazon hiring insights
CodePython 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import heapq
from collections import defaultdict
 
class Solution:
def solve(self, values, queries):
# Build the adjacency structure
n = len(values)
graph = defaultdict(list)
for u, v in queries:
graph[u].append(v)
 
best = [float("inf")] * n
heap = [(0, 0)]
while heap:
cost, node = heapq.heappop(heap)
if cost > best[node]:
continue
Run and Submit unlock with Pro
FastPrep Pro
Reported in 1 Amazon interview this week

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
$8.25/month

$99 billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week