Number of Islands
Reported by candidates from Tekion's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The edge case that breaks a naive Number of Islands solution is diagonal land, and Tekion's July 2025 OA version gives you strings instead of a char matrix to make you trip on it. Grid is an array of strings, '1' is land, connections are horizontal and vertical only. It's the classic flood fill, and the pattern is depth-first or breadth-first search on a matrix. If you've seen it, you'll be fine. If your mind goes blank at the clock, StealthCoder runs invisibly during the live assessment and gives you a working solution as a safety net.
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 countIslands(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, the center cell, and the lower-right pair form three separate islands. Example 3 grid = ["000","000"] return = 0 The matrix contains no land cells. Constraints 1 <= grid.length <= 300 1 <= grid[i].length <= 300 Every row has the same length and contains only '0' and '1'.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Scan every cell. When you hit a '1' that hasn't been visited, increment the count and flood-fill the whole island so you never count it again. Use DFS or BFS with four directions only: up, down, left, right. Diagonals don't connect, and Example 2 proves it with the center cell standing alone. The common pitfalls: strings are immutable, so you can't write '0' into the grid directly. Use a separate visited boolean array, or convert rows to char arrays first. Recursive DFS on a 300 by 300 grid can go 90,000 frames deep and blow the stack in some languages, so an iterative stack or BFS queue is safer. Time is O(rows times cols). If you freeze on the live OA, StealthCoder is the hedge that hands you the iterative version.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
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. 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 number of islands. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Tekion's OA.
Tekion 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.
Number of Islands FAQ
What's the trick to Number of Islands in the Tekion OA?+
Treat each unvisited '1' as the start of a new island, count it, then flood-fill every connected land cell using DFS or BFS. Mark cells visited so nothing gets counted twice. Only four directions count, never diagonals.
How hard is this problem really?+
It's a medium on paper but one of the most common grid problems out there. The logic is short. Most failures come from bounds checks, diagonal moves, or trying to mutate an immutable string row.
Why does the input come as an array of strings?+
The function takes String[] grid, so each row is a string like "11110". You can read grid[r][c] but usually can't assign to it. Keep a visited matrix or copy each row into a mutable char array before you start.
Should I use DFS or BFS with a 300 by 300 grid?+
Either works at O(rows times cols). Recursive DFS can reach 90,000 depth on one big island, which risks a stack overflow in some languages. BFS with a queue or DFS with an explicit stack avoids that and is the safer pick.
How do I prepare for this in 48 hours?+
Write the solution from scratch twice, once with BFS and once with iterative DFS. Test the three examples plus an all-land grid and a single-cell grid. Check that your code treats diagonal land as separate islands and handles an all-water grid by returning 0.