Battleships in a Board
A medium-tier problem at 77% community acceptance, tagged with Array, Depth-First Search, Matrix. Reported in interviews at Tinkoff and 1 others.
You're given a 2D board with battleships already placed, and you need to count how many exist without revisiting the same ship twice. It's a matrix traversal problem that looks straightforward until you realize the naive flood-fill approach wastes time. Tinkoff and Microstrategy have both asked it. The trap is treating it like a connected-components problem when there's actually a much faster one-pass solution that most candidates miss. If this hits your live assessment and you blank on the optimization, StealthCoder surfaces the working approach in seconds while you stay invisible to the proctor.
Companies that ask "Battleships in a Board"
Battleships in a Board is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an engineer who got tired of watching his cohort grind for six months and still get filtered at the OA stage.
Get StealthCoderThe trick here is recognizing that you don't need DFS or BFS at all. Since battleships are axis-aligned and non-overlapping, you can count them by iterating through the board once and only counting the 'top-left' cell of each ship. When you hit an 'X', check if the cell above it or to its left is also 'X'. If neither is, you've found a new battleship. That's it. One pass, no recursion, no visited set. Most candidates waste effort building a full DFS solution when the problem structure actually forbids overlapping or diagonal ships. That constraint is the key. If you code defensive nested DFS anyway, you'll time out on large boards. StealthCoder catches this pattern instantly and hands you the linear solution, which is why the acceptance rate is solid at 77 percent but also why many interviews go sideways on the implementation.
Pattern tags
You know the problem.
Make sure you actually pass it.
Battleships in a Board recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an engineer who got tired of watching his cohort grind for six months and still get filtered at the OA stage. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Battleships in a Board interview FAQ
Is this really just a 'count connected components' problem?+
No. That's the trap. Connected components require DFS or BFS, which are slower. Since battleships are axis-aligned, non-overlapping, and guaranteed by the problem constraints, you count only the top-left corner of each ship in one pass. No traversal needed.
Why do Tinkoff and Microstrategy ask this if it's so short to code?+
Because the optimization gap is wide. Many candidates code the 'obvious' graph traversal and then realize it's overkill. The interview probes whether you read constraints carefully and optimize before coding. That mindset matters at scale-focused companies.
What's the actual time complexity of the optimal solution?+
O(m*n) single pass through the board, where m and n are dimensions. Space is O(1) constant. Most DFS solutions also hit O(m*n) time but waste space on recursion and visited sets. The one-pass trick is cleaner and faster in practice.
How does this relate to Depth-First Search if DFS isn't the answer?+
DFS is listed as a topic because many platforms categorize problems by the naive first approach a candidate might try. The optimal solution sidesteps DFS entirely by exploiting the axis-aligned, non-overlapping constraint. Understanding when to reject a textbook approach is the real skill here.
What happens if I code a full DFS solution in the interview?+
You'll get it right, but slower. It shows you can traverse a matrix, which is fine. But interviewers at companies like Tinkoff will ask follow-ups like 'Can you do better?' That's your cue to pivot to the one-pass corner-check trick and show you optimize based on problem structure.
Want the actual problem statement? View "Battleships in a Board" on LeetCode →