FastPrepCount Battleships by Size
Problems/Count Battleships by Size

Count Battleships by Size

Deloitte logoDeloitte● EasyFULLTIMEOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Deloitte online assessment. The report leaves out the board symbols, the ship shapes, what counts as adjacent, and the order of the output. Our version is about 78% the same as the reported question.

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]
grid
0123450#..##.1......2###...3......

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]
grid
0120#.#1..#2..#3...4##.

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]
grid
012340#.#.#1..#.#2....#3##...4.....

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.

More Deloitte Online Assessment questions

  • Seventh Nearest PalindromeOA · Seen May 2026
  • Minimum Deletions for a Non-PalindromeOA · Seen Jul 2025
  • Minimum-Cost Solar Rooftop AllocationOA · Seen Jul 2025
  • Pair Elements with Even SumsOA · Seen Jul 2025
  • Count Divisible PermutationsOA · Seen Jan 2025
  • Largest Element in an ArrayOA · Seen Nov 2024
  • Install Carbon FiltersOA · Seen Oct 2023
  • Longest Substring with Even OccurrencesOA · Seen Oct 2023
  • Maximum Sum RectangleOA · Seen Apr 2022
  • Annual Bank Balance with Monthly Card FeesOA · Seen Jan 2022

Similar questions at other companies

  • Reveal a Minesweeper RegionNuro · PHONE SCREENNuro interview questions
  • Wind-Blown Leaves Remaining in a GridAbridge · OAAbridge interview questions
  • Invert or Blur an ImageOpenAI · OAOpenAI interview questions
  • Count Pieces After Othello MovesHudson River Trading · OAHudson River Trading interview questions
  • Trapped Characters in a GridAdobe · OAAdobe interview questions
  • Simulate a Deterministic Finite AutomatonPoint72 · OAPoint72 interview questions
  • Connected GroupsVisa · OAVisa interview questions
  • Collect Letters Along a Unique Grid PathApple · OAApple interview questions
  • Find Largest House AreaHSBC · OAHSBC interview questions
  • Rover MoveIBM · OAIBM interview questions

Deloitte interview experiences

  • Deloitte New Grad Tech OA and Interview Questions: 14 Experiences
All Deloitte interview questionsSee Deloitte hiring insights
Saved
class Solution {
    public int[] countShipTypes(String[] grid) {
        // Write your code here.
    }
}
Checking account…