Reported August 2024
Highspotdepth first search

Number of Islands

Reported by candidates from Highspot's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Highspot OA. Under 2s to a working solution.
Founder's read

Highspot reportedly served up Number of Islands in August 2024, and the grid can hit 300 by 300. That's 90,000 cells, so any approach that rescans the grid for every land cell will crawl. The pattern is a flood fill: walk the grid once, and each time you hit unvisited land, count one island and mark everything connected to it. It's a classic, which means the OA is testing clean execution, not creativity. If you blank on the traversal mid-assessment, StealthCoder runs invisibly as a safety net and reads the problem for you. Here's the script.

The problem

Given an array of equal-length strings grid, where '1' represents land and '0' represents water, return the number of islands.
An island is a maximal group of land cells connected horizontally or vertically. Cells outside the matrix are water.

Function
numIslands(grid: String[]) → int

Examples
Example 1
grid = ["11110","11010","11000","00000"]
return = 1
All land cells belong to one orthogonally connected component.
Example 2
grid = ["11000","11000","00100","00011"]
return = 3
The upper-left block, center cell, and lower-right pair are separate islands.
Example 3
grid = ["000","000"]
return = 0
The grid contains no land cells.

Constraints
1 <= grid.length <= 300.
1 <= grid[i].length <= 300, and every row has the same length.
Every character is '0' or '1'.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is that each land cell gets visited exactly once. Scan every cell. When you find a '1', increment the count, then run DFS or BFS in four directions (up, down, left, right) and flip every reached '1' to '0', or track a visited set. That makes it O(rows * cols) time. The pitfalls are specific to this version. The grid is an array of strings, and strings are immutable in most languages, so convert rows to char arrays or use a separate visited matrix. Don't include diagonals. Check bounds before indexing. With 300 by 300, recursive DFS can hit stack depth limits in some languages, so BFS with a queue or an explicit stack is safer. If you freeze on those details during the live OA, StealthCoder is the hedge that gives you a working solution on screen without the proctor seeing it.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as number of islands. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Highspot's OA.

Highspot reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Number of Islands FAQ

How hard is Number of Islands really?+

It's medium on paper, but it's one of the most common grid problems. If you've written a flood fill before, it takes 10 to 15 minutes. The difficulty is in the details: bounds checks, four-direction movement, and not double-counting cells.

What's the trick to solving it fast?+

Scan the grid once. On each unvisited '1', add one to the count and flood-fill the whole connected component so you never count it again. Mark cells as visited by overwriting them or using a boolean matrix. Every cell is touched a constant number of times.

DFS or BFS for a 300 by 300 grid?+

Both give the same O(rows * cols) time. A worst-case all-land grid makes recursive DFS go 90,000 frames deep, which can overflow the stack in some languages. BFS with a queue or iterative DFS with a stack avoids that risk entirely.

Why can't I just modify the input strings?+

The input is an array of strings, and strings are immutable in many languages. Either convert each row into a character array first, or keep a separate visited matrix of the same size. Pick one before you start coding to avoid a mid-solution rewrite.

How do I prepare in 48 hours?+

Write the solution from scratch twice, once with DFS and once with BFS. Then test the edge cases from the examples: a single island, separate blocks, and an all-water grid. Also try a 1 by 1 grid. That covers nearly everything this problem can throw at you.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Highspot.

OA at Highspot?
Invisible during screen share
Get it