Number of Islands
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Brute force dies on this one the second the grid gets big, and Amazon's Number of Islands, reported in October 2026, is built to punish anyone who rescans the grid for every land cell. The input is an array of equal-length strings with '1' for land and '0' for water, and you count orthogonally connected groups of land. It's a connected-components problem dressed as a grid. If you blank during the live OA, StealthCoder runs invisibly as a safety net and hands you the traversal. Know the pattern first and you probably won't need it.
The problem
Given an array of equal-length strings grid, where '1' represents land and '0' represents water, return the number of islands. Examples Example 1 grid = ["11110","11010","11000","00000"] return = 1 All land cells belong to one orthogonally connected component.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is one pass over every cell. When you hit a '1', increment the island count, then flood fill from that cell with DFS or BFS so every connected land cell gets marked visited. Later cells in the same island are skipped, so each cell is touched a constant number of times and the whole thing runs in O(rows * cols). The common pitfalls: strings are immutable, so you can't write '0' into the grid directly unless you convert to char arrays or keep a separate visited set. Forgetting bounds checks is the other classic. Diagonals don't count, only up, down, left, right. Deep recursion on a huge all-land grid can overflow the stack, so an iterative BFS or explicit stack is safer. If you freeze mid-assessment, StealthCoder is the hedge that gives you the working flood fill in real time.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Number of Islands cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as number of islands. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Number of Islands FAQ
How hard is Number of Islands really?+
It's a medium and one of the most common grid problems. The logic is short, but the bugs are in bounds checks and visited tracking. If you've written a flood fill once, you can finish this in a few minutes.
What's the trick for the Amazon version?+
Scan every cell. On each unvisited '1', count one island and flood fill its whole connected group with DFS or BFS. Mark cells visited so you never count them twice. That's the entire idea.
Do I need to modify the input grid?+
No. Since the grid is given as strings, which are immutable in many languages, use a visited boolean matrix or a set of coordinates. Converting rows to character arrays also works if mutation is fine.
DFS or BFS, which should I pick?+
Either works with the same complexity. DFS recursion is shorter to write but can overflow the stack on large grids. BFS with a queue is safer and just as quick to code. Pick whichever you can write without bugs.
How do I prepare in 48 hours?+
Write the flood fill from scratch twice, once DFS and once BFS. Then do one variant, like max island area. Test an empty grid, a single cell, and an all-land grid. That covers what an Amazon OA will throw at you.