Reported January 2022
Deloittematrix

Count Battleships by Size

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

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

The data structure this one hinges on is just the grid itself, plus a visited marker. Deloitte reported this OA in January 2022, and it's a connected-components count dressed up as Battleship. You scan a board of # and. cells, find each ship, measure it at length 1, 2, or 3, and return the counts as [single, double, triple]. It's easy to overthink and easy to botch on edge cases. If you blank during the live assessment, StealthCoder runs invisibly on your desktop, reads the problem, and hands you a working solution so one bad moment doesn't sink the attempt.

The problem

You are given a rectangular Battleship board as an array of equal-length strings grid:
# is part of a ship.
. is water.
Each ship is one maximal group of # cells connected vertically or horizontally. Every ship is guaranteed to be a straight horizontal or vertical segment of length 1, 2, or 3. Distinct ships do not touch vertically or horizontally, although they may touch diagonally.
Return an integer array [single, double, triple], where:
single is the number of length-1 ships.
double is the number of length-2 ships.
triple is the number of length-3 ships.

Function
countShipTypes(grid: String[]) → int[]

Examples
Example 1
grid = ["#..##.","......","###...","......"]
return = [1,1,1]
The board contains one isolated cell, one horizontal ship of length 2, and one horizontal ship of length 3.
Example 2
grid = ["#.#","..#","..#","...","##."]
return = [1,1,1]
The board contains a single-cell ship, a horizontal length-2 ship, and a vertical length-3 ship.
Example 3
grid = ["#.#.#","..#.#","....#","##...","....."]
return = [1,2,1]
There is one length-1 ship, two length-2 ships, and one length-3 ship. Diagonal contact does not merge ships.

Constraints
1 <= grid.length <= 500.
1 <= grid[i].length <= 500.
Every row has the same length.
Every cell is # or..
Every maximal orthogonally connected ship is a straight segment of length 1, 2, or 3.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is that ships are guaranteed straight and never touch orthogonally, so you don't need a full flood fill. Scan the grid row by row. When you hit a # that hasn't been visited, that's the top-left end of a new ship. Look right and down to measure its length, then mark those cells visited (or overwrite them with '.'). Increment the matching counter. A DFS or BFS flood fill also works and is safer if you worry about the guarantee. Common pitfalls: counting a ship once per cell, forgetting that diagonal contact does not merge ships, and indexing out of bounds at the edges. With a 500 by 500 board, one linear pass is plenty fast. Avoid recursion if you use DFS, since a deep stack isn't needed when ships max out at length 3. If you freeze on the OA, StealthCoder is the hedge that keeps you moving.

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 Count Battleships by Size 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

⏵ The honest play

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

Deloitte 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.

Count Battleships by Size FAQ

How hard is Count Battleships by Size really?+

Easy to medium. It's a grid scan with a small twist: you count ships by length instead of just counting them. If you've seen the classic Battleships in a Board problem, you already know 90 percent of it.

What's the trick to solving it fast?+

Treat each unvisited # as the start of a new ship, then measure its length by walking right and down. Mark those cells visited so you never count them twice. Ships are straight and never touch orthogonally, so no complex traversal is needed.

Should I use DFS, BFS, or a plain scan?+

A plain scan is the cleanest, but DFS or BFS flood fill is fine and more general. Count the component size, then bump single, double, or triple. Pick whichever you can write without bugs under pressure.

What edge cases break most solutions?+

Double counting a ship cell by cell, going out of bounds on the last row or column, and merging ships that only touch diagonally. Test a one-cell grid, an all-water grid, and a board with diagonal neighbors like Example 3.

How do I prepare for this in 48 hours?+

Write the grid scan with a visited array twice from memory. Then run the three examples by hand, especially Example 3. Also rehearse a flood-fill version of connected components so you have a fallback if your first approach stalls.

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

OA at Deloitte?
Invisible during screen share
Get it