Count Islands with Eight-Direction Adjacency
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The data structure carries this one: either a visited set with a stack or queue, or a union-find over the cells. Amazon candidates reported this count-islands variant in September 2026, and the twist is eight-direction adjacency, so diagonals merge land. Example 1 shows it: five cells in an X shape collapse into one island. It's a matrix traversal problem with a small catch, and it's very doable if you've seen the classic. If you blank during the live OA, StealthCoder runs invisibly as a safety net and gives you the working solution. Here's the pattern so you don't need it.
The problem
Given a rectangular binary matrix grid, return the number of islands. An island is a maximal group of cells containing 1. Two land cells belong to the same island when they share an edge or a corner, so each cell can connect in any of the eight horizontal, vertical, or diagonal directions. Function countIslands8(grid: int[][]) → int Examples Example 1 grid = [[1,0,1],[0,1,0],[1,0,1]] return = 1 Every land cell connects to the center diagonally, so all five cells form one island. Example 2 grid = [[1,1,0],[0,0,0],[0,1,1]] return = 2 The upper-left and lower-right land groups do not touch, even at a corner. Example 3 grid = [[0,0],[0,0]] return = 0 There are no land cells. Constraints 1 <= grid.length, grid[i].length <= 500. Every row has the same length. grid[i][j] is either 0 or 1. The matrix contains at most 100000 cells.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is flood fill. Scan every cell. When you hit an unvisited 1, increment the count and traverse every connected land cell, marking each one visited. The only change from the classic problem is the neighbor list: eight (dr, dc) pairs instead of four. The common pitfall is recursion depth. With up to 100000 cells, a single island can be one huge blob, and recursive DFS can overflow the stack in some languages. Use an explicit stack or a BFS queue instead. Mark cells visited when you push them, not when you pop them, or you'll enqueue duplicates and slow down. You can flip 1 to 0 in place if mutating the input is allowed, otherwise keep a boolean array. Time is O(rows*cols), space is O(rows*cols) worst case. If you freeze live, StealthCoder is the hedge that reads the prompt and hands you this approach.
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 Count Islands with Eight-Direction Adjacency 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
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.
Count Islands with Eight-Direction Adjacency FAQ
What's the trick in this Amazon island-counting problem?+
Eight-direction adjacency. Diagonal land cells belong to the same island, so your neighbor list needs all eight offsets. Everything else is standard flood fill. Count one island each time you start a fresh traversal from an unvisited 1.
Should I use DFS, BFS, or union-find?+
BFS or iterative DFS is simplest and safest. Union-find works too but takes more code for the same O(n) result. Pick the one you can write without bugs in a few minutes. Traversal wins for most people.
Will recursive DFS pass the 500 by 500 constraint?+
It's risky. One island can span up to 100000 cells, and recursion that deep can overflow the stack in languages with small default limits. Use an explicit stack or a queue to avoid the problem entirely.
How do I avoid counting an island twice?+
Mark cells as visited the moment you add them to the stack or queue. Either flip the cell from 1 to 0 in place or use a separate visited array. Only increment the counter when you start from an unvisited 1.
How do I prepare for this in 48 hours?+
Write the classic four-direction island counter from scratch, then swap in eight directions. Test on the three examples, including the all-zero grid and the X-shaped one. Check edge cases like a 1 by 1 grid. That's enough for this pattern.