Maximum Area of Island
Reported by candidates from SambaNova Systems's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt at this one is counting cells twice. SambaNova Systems reported Maximum Area of Island in June 2022, and it's a grid flood-fill problem in a clean wrapper. You get a binary matrix, you find the biggest group of 1s connected up, down, left or right, and you return its size. Empty grid returns 0. It's a BFS or DFS question, and the code is short. The risk isn't the idea, it's blanking on bookkeeping under a timer. StealthCoder sits invisibly on your screen during the live OA as a safety net if that happens.
The problem
Given a rectangular binary matrix grid, return the maximum area of an island. An island is a maximal group of cells containing 1 connected horizontally or vertically. Its area is its number of cells. Return 0 when the matrix contains no land. Function maxIslandArea(grid: int[][]) → int Examples Example 1 grid = [[0,0,1,0],[1,1,1,0],[0,1,0,1]] return = 5 The center island contains five orthogonally connected land cells. The bottom-right cell is a separate island of area 1. Example 2 grid = [[0,0],[0,0]] return = 0 There are no land cells. Constraints 1 <= grid.length, grid[i].length <= 300. Every row has the same length. grid[i][j] is either 0 or 1.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to treat every unvisited 1 as the start of a flood fill. Run BFS or DFS from it, count cells as you pop them, and track the max across all starts. Mark cells visited the moment you push them, not when you pop them. That's the classic first-attempt bug: mark late and the same cell gets queued several times, inflating the area. Simplest fix is to overwrite the cell with 0 when you enqueue it, so no separate visited array is needed. Only check four directions, not diagonals, since the problem says horizontal or vertical. With a grid up to 300 by 300, that's 90,000 cells, so recursive DFS can hit stack limits in some languages. Use an explicit stack or queue to be safe. Time is O(rows * cols). If you freeze mid-OA, StealthCoder can hand you the iterative version in real time.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Maximum Area of Island 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as max area of island. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass SambaNova Systems's OA.
SambaNova Systems reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Maximum Area of Island FAQ
What's the trick to Maximum Area of Island?+
Flood fill from every unvisited 1, count the cells in that component, and keep the maximum. Each cell is processed once, so it runs in O(rows * cols). The only real work is marking cells visited correctly so nothing gets counted twice.
Should I use BFS or DFS here?+
Either works and both give the same answer. BFS with a queue avoids recursion depth problems on a 300 by 300 grid. If you're fast with recursive DFS, it's fine, but an iterative stack is the safer choice if the language has a low recursion limit.
What bug breaks most first attempts?+
Marking visited too late. If you only mark a cell when you pop it, neighbors can push it again and the area gets overcounted. Mark on push, or set the cell to 0 immediately when you enqueue it.
Do diagonals count as connected?+
No. The problem says connected horizontally or vertically, so you check exactly four directions. In Example 1 the bottom-right 1 is its own island of area 1, which confirms diagonals don't connect.
How do I prepare for this in 48 hours?+
Write the iterative BFS version from scratch twice. Test it on the all-zero grid, a single cell grid, and a grid that's all 1s. Those three cases catch bounds errors and the return-0 requirement. Then you're done, this pattern is short.