Design Battleship
🚢 What is Battleship? It is a two-player guessing game. Each player hides a fleet of ships on their own grid. Then they take turns calling out a cell on the other player's grid and hear back "miss", "hit" or "sunk". The first player to sink the whole enemy fleet wins.
Battleship looks like tic-tac-toe with more cells, but it tests different things. There are two boards, not one, and each player can see only part of the truth. The game has two clear phases with different allowed actions. And the most important question, "did this shot sink a ship?", is easy to answer slowly and just as easy to answer instantly if you pick the right data structure. That choice is what most interviewers want to see.
Requirements
"Design the game Battleship. Two players place their ships on a grid and then take turns firing at each other."
The prompt leaves the board size, the fleet and the turn rules open, and each has popular variants. The questions below pin down one version.
Clarifying Questions
You: How big is the grid, and which ships does each player get?
Interviewer: Take both as input. The classic game is 10 by 10 with ships of length 5, 4, 3, 3 and 2. Both players get the same fleet.
You: How are ships placed? Can they touch or overlap?
Interviewer: Each ship is a straight line, horizontal or vertical, fully on the grid. A player's ships may touch but not overlap. Reject a bad placement and let the player try again.
You: When does the shooting start? Can a player fire before the other has placed their ships?
Interviewer: No. The battle starts once both fleets are complete. Player 1 fires first.
You: Does a hit give the shooter another shot?
Interviewer: No, the players simply alternate after every shot. Some people play "hit and go again", but not here.
You: What happens if someone fires at a cell they already tried, or off the grid?
Interviewer: Reject it and let the same player pick another cell. It should not cost them their turn.
You: What does the shooter learn from a shot?
Interviewer: Miss, hit, or sunk when the shot finishes off a ship. And they should hear that they won when the last ship goes down.
The fourth and fifth answers are the turn rules, and they decide the tests: a repeat shot is not a miss. The last answer tells us that "sunk" must be known at the moment of the shot, which is the requirement that shapes the board.
💡 Interview tip: For any game, ask what happens on an invalid action: is it an error, a lost turn, or a retry? The answer changes the turn logic, and interviewers often have a specific one in mind.
Final Requirements
- The game has two players, each with their own
n x ngrid, and a fleet given as a list of ship lengths. Each player places one ship of each length in the fleet. - A ship is a straight horizontal or vertical line, fully on the grid, that does not overlap the player's other ships. A bad placement is rejected and changes nothing.
- The battle starts when both fleets are fully placed. No ships can be placed after that, and no shots can be fired before.
- Player 1 fires first; the players alternate after every accepted shot.
- A shot off the grid, at an already-tried cell, or out of turn is rejected and does not change the turn.
- Every accepted shot reports miss, hit or sunk. The shot that sinks the opponent's last ship reports a win and ends the game.
- Anyone can ask how many of a player's ships are still afloat.
Out of scope
- Drawing the grids and hiding the ships from the other player's screen (we come back to this in Extensibility).
- A computer opponent (also in Extensibility).
- Networking, matchmaking and saving games.