Battleship Game Logic & Ship Placement
- Model a Battleship board, ships and game as small classes with clear jobs
- Validate ship placement for bounds, direction and overlap before writing anything
- Resolve a shot as miss, hit or sunk in constant time with per-ship counters
- Run two players with turns, wasted moves and a clean game over
If you have a GitHub software engineer interview coming up, there's a fair chance one of your coding rounds is Battleship. A SWE I candidate in February 2025 was told ahead of time to expect "a battleship question and an API question." A SWE II candidate in March 2026 opened interview day with "Build a game of battleship." And a senior candidate in November 2026 got it as the second of two coding rounds, described as game logic for shooting at warships on a 2D grid, with an object-oriented design expected.
Grid size, ship lengths and fleet size change from loop to loop and are often left for you to settle, so the version here is something like what candidates describe. Expect to agree on the rules out loud. What stays constant is the shape. You build the board and ships, check placement, fire a shot and say whether it missed, hit or sank something, then handle turns and the end of the game.
What the GitHub Battleship round asks
Interviewers often leave the rules loose, so expect to pin them down yourself. Spend the first few minutes on that. The classic game has ships of fixed lengths lying horizontally or vertically without overlapping. Each shot misses, hits, or hits and sinks, and you win by sinking the whole opposing fleet. Then ask about the edges, because each answer changes your code:
- How big is the grid, and how many ships of which lengths? Are they fixed or passed in?
- Who places the ships: the players, the program at random, or the caller as input?
- Two players taking turns, or one player shooting at a hidden fleet?
- What happens on a shot outside the grid, or at a cell already fired at?
- Does a hit say which ship it hit, or is a ship named only when it sinks?
Write the answers as comments at the top of your file. When a test or a follow-up depends on one of them, you can point at it instead of arguing from memory.
Three classes cover it. A Ship knows its cells and how many of them are hit. The Board maps each occupied cell to its ship and remembers every shot, and the Game owns two boards and whose turn it is. Keep a hit count on each ship and a count of ships still afloat on each board, and sunk and game over are O(1) checks with no scan of the grid. It's the same idea as the row and column counters in LeetCode 348, Design Tic-Tac-Toe.
Board, ships and placement
Placement comes first in any version, and a lot of bugs hide there. A ship that runs off the edge, sits on another ship, or reuses a name has to be rejected before it touches the board. The class below takes the fleet one ship at a time, the way a player drags pieces onto the grid. It returns a short reason when a placement fails, so the tests can tell the failures apart.
Battleship Board With Ship Placement Validation
Problem
Board with rows rows and cols columns. Cell (0, 0) is the top-left corner, rows grow downward and columns grow to the right.place_ship(name, row, col, length, direction) puts a ship whose first cell is (row, col). Direction "H" runs right along the row and "V" runs down the column. It returns "placed" on success. Otherwise it returns one reason and changes nothing, checked in this order: "duplicate name", then "invalid ship" for a length below 1 or any other direction, then "out of bounds", then "overlap" when any cell already holds a ship.ship_at(row, col) returns the name of the ship on that cell, or "" for open water.- Board(5, 5)
- place_ship("destroyer", 0, 0, 2, "H")→ "placed"
- place_ship("sub", 1, 4, 3, "V")→ "placed"
- ship_at(0, 1)→ "destroyer"
- ship_at(3, 4)→ "sub"
- ship_at(2, 2)→ ""
The destroyer covers (0,0) and (0,1). The sub runs down column 4 from row 1 to row 3. Cell (2,2) is water.
Compute the list of cells the ship would cover first. Check every cell against the bounds and against a dict from cell to ship name, and only write once all the checks pass.
Battleship Board With Ship Placement Validation
Click "Run" to execute your code
Firing a shot: miss, hit or sunk
Firing is the core of the game. The November 2026 senior round was described as game logic for shooting at warships, and resolving a shot is that logic. A shot misses, hits, or sinks the ship it lands on, and the board has to know when its last ship goes down. Here one player shoots at a hidden fleet, with a Ship class carrying its own hit count.
Battleship Shots That Report Miss Hit or Sunk
Problem
Board from the previous problem. place_ship works exactly as before, with the same reasons in the same order.fire(row, col) returns "out of bounds" for a cell outside the grid and "already fired" for a cell shot before. Neither changes anything. Otherwise it records the shot and returns "miss" on water, "hit" on a ship that still has unhit cells, or "sunk " when that shot hits the ship's last unhit cell.ships_left() returns how many placed ships aren't sunk yet.- Board(5, 5)
- place_ship("destroyer", 0, 0, 2, "H")→ "placed"
- fire(0, 0)→ "hit"
- fire(4, 4)→ "miss"
- fire(0, 1)→ "sunk destroyer"
- ships_left()→ 0
The second hit on the two-cell destroyer sinks it, and no ships are left.
Give each ship a hit counter and the board a count of ships afloat. A shot is a set check for repeats, one dict lookup for the ship, and a comparison of hits to length.
Battleship Shots That Report Miss Hit or Sunk
Click "Run" to execute your code
Two players, turns and game over
The full game puts two boards together and adds whose turn it is. Turns and game over close out the game logic, and they're where rule questions pile up. You have to decide when placement closes, whether a wasted shot costs the turn, and what a move after the game ends returns. The version here answers each one in a single place, the Game class, and leaves the board logic untouched.
Two-Player Battleship With Turns and Game Over
Problem
Game for players 1 and 2, each with their own rows by cols board. place_ship(player, name, row, col, length, direction) places a ship on that player's own board with the rules and results of the first problem. It returns "invalid player" for any other player number, then "game started" once the game has started.fire(player, row, col) shoots at the other player's board. Check in this order: "game over" once someone has won, "invalid player", then "not ready" while either board has no ships. The first shot that gets past those starts the game. Then "not your turn"; player 1 shoots first.The shot result is the one from the previous problem. A miss, hit or sink passes the turn.
"out of bounds" and "already fired" keep it. The shot that sinks the opponent's last ship returns "player wins" . get_winner() returns the winner, or 0.- Game(4, 4)
- place_ship(1, "boat", 0, 0, 2, "H")→ "placed"
- place_ship(2, "boat", 3, 0, 2, "H")→ "placed"
- fire(1, 3, 0)→ "hit"
- fire(1, 3, 1)→ "not your turn"
- fire(2, 0, 0)→ "hit"
- fire(1, 3, 1)→ "player 1 wins"
- get_winner()→ 1
- fire(2, 0, 1)→ "game over"
Player 1 hits, then tries again out of turn. The second player hits back, and then player 1 sinks the only ship on board 2 and wins, so the next shot is refused.
Compose two Board objects from the previous problem, one per player. The Game only adds a turn number, a started flag and a winner, and decides which results pass the turn.
Two-Player Battleship With Turns and Game Over
Click "Run" to execute your code
The senior follow-ups: running it as an online game
The November 2026 senior round didn't end at game over. After the object-oriented part, the interviewer asked about production deployment and about the performance and system issues you'd see running it online. Prepare the questions that come straight out of the code you just wrote.
Right now your Game lives in one process's memory, and production breaks that quickly. State has to survive a restart or a deploy. Two requests for the same game can land at once, and a client may retry a shot it already sent. A client must never learn where the other fleet sits either, so the server holds the only real copy of the game and sends each player their own board plus the results of their own shots. The design lesson Design Battleship as an Online Game works through storage, concurrency, idempotent moves and deploys in full.
The 2017 take-home version
Battleship isn't new at GitHub. Between February and July 2017 it was the take-home coding challenge. A March 2017 version had this spec: 3 ships on a 10 by 10 grid, with [0,0] as the bottom-left cell. Each ship is 1 cell wide and 3 long, placed vertically, and the game is created from a JSON array of each ship's topmost cell. It ran as a basic HTTP API whose responses carried one message, "hit", "miss" or "sunk".
A senior candidate that March implemented it inside a pre-made Rails app with a two-hour limit, and an April 2017 senior candidate built it as an API for playing the game and submitted it as a pull request. The format has moved to a live round since. The 2017 spec is still a useful drill, because its fixed rules strip the problem to coordinates and sinking.
Battleship 2017 Take-Home With Bottom-Left Coordinates
Problem
BattleshipGame on a 10 by 10 grid. Cells are (x, y) with (0, 0) at the bottom-left, so y grows upward. There are always 3 ships, each 1 cell wide and 3 cells long, standing vertically.create(tops) takes a list of 3 [x, y] pairs, the topmost cell of each ship. A ship with top [0, 3] covers (0, 3), (0, 2) and (0, 1). It returns "invalid" if there aren't exactly 3 ships, a ship leaves the grid, or two ships share a cell. An invalid fleet leaves any current game as it was. Otherwise it starts a fresh game and returns "ok".update(x, y) fires at a cell. It returns "invalid" before a successful create or for a cell off the grid, "miss" on water, "sunk" when all 3 cells of the ship at that cell are hit, and "hit" otherwise. Firing at a cell again never counts as a new hit.- BattleshipGame()
- create([[0, 3], [4, 8], [6, 6]])→ "ok"
- update(0, 1)→ "hit"
- update(0, 2)→ "hit"
- update(5, 5)→ "miss"
- update(0, 3)→ "sunk"
The first ship covers (0,3), (0,2) and (0,1). Hitting all three, with a miss in between, sinks it.
Expand each top cell into its three cells by subtracting 0, 1 and 2 from y. Map each cell to a ship index, and give each ship a set of the cells hit so far.
Battleship 2017 Take-Home With Bottom-Left Coordinates
Click "Run" to execute your code
What earns points in the Battleship round
GitHub doesn't publish a rubric for this round, but its shape shows what earns points. The senior round asked for object-oriented design and then for production thinking, and both reward the same habits. Pin down the rules before you code and write them where everyone can see them. Give each class one job: the ship knows its cells and hits, the board resolves shots, and the game owns turns. Keep shot resolution and game over constant-time with counters.
Common questions
Does GitHub still ask Battleship in its coding interview?
Yes. It appears at SWE I in February 2025, SWE II in March 2026 and senior in November 2026, and the SWE I candidate was told the topic beforehand. It was also the take-home in 2017. Not every loop has it. Other recent rounds include an HTTP or REST API round, a JSON query engine and a one-problem online assessment.
Is GitHub's Battleship question a LeetCode problem?
There's no LeetCode equivalent. It's an object-oriented build. The closest practice is LeetCode 348, Design Tic-Tac-Toe, which trains the same habit of keeping counters so a win check doesn't scan the board.
What grid size and fleet should I assume?
It varies, so ask. If the interviewer leaves it to you, take the grid size and the fleet as parameters and say why. The one fixed spec GitHub has used is the 2017 take-home, with 3 ships of length 3 on a 10 by 10 grid.
How should I practice it?
Build the three parts above from a blank file on a timer, testing each part before you start the next. Then rehearse the senior follow-ups out loud with the online game design lesson, since the November 2026 senior round went there right after the code.
Key Takeaways
- ✓Battleship shows up in GitHub coding rounds at SWE I, SWE II and senior in 2025-2026. Agree the rules before coding.
- ✓Validate the whole ship before writing any cell, using a dict from cell to ship for bounds and overlap.
- ✓Per-ship hit counts and an afloat count make miss, hit, sunk and game over constant-time.
- ✓Keep turn rules in the Game class, and expect senior follow-ups on running the game online.
GitHub interview guideKnow what GitHub asks before you walk in.
20 coding · 3 system design · 31 behavioral questions
Get their questions first.
InterviewLegend is independent and not affiliated with or endorsed by any company it covers.