Design a Battleship game simulation with ship placement, shots, sinks and win detection
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a simulation of the classic Battleship game on a single rectangular grid of size `rows x cols`. The board starts empty.
Players first place ships of a given length, either horizontally or vertically. Ships cannot overlap and must fit entirely inside the board. Each placed ship is assigned a unique incrementing `shipId` starting from 0.
Players then fire shots at coordinates. A shot is a miss if no ship occupies the cell, a hit if it damages a ship that still has unhit cells afterward, a sink if it destroys the last remaining cell of a ship, and a win when it sinks the final ship on the board. Firing twice at the same cell always returns `miss`.
Implement the class `BattleshipGame` with these methods:
- `BattleshipGame(rows: int, cols: int)` creates an empty board.
- `placeShip(row: int, col: int, length: int, orientation: str) -> int` places a ship of the given length starting at (row, col) and extending horizontally (`'H'`, to the right) or vertically (`'V'`, downward). Returns the assigned shipId (starting from 0), or -1 if the placement is invalid (out of bounds or overlapping an existing ship).
- `fireShot(row: int, col: int) -> str` fires a shot at (row, col). Returns `'miss'` if no ship is hit or the cell was already fired upon, `'hit'` if a ship is hit but not sunk, `'sink'` if this shot sinks a ship but ships remain, or `'win'` if this shot sinks the last remaining ship.
- `remainingShips() -> int` returns the number of ships that have not yet been fully sunk.
Example 1:
```text
Input:
["BattleshipGame","placeShip","placeShip","placeShip","fireShot","fireShot","remainingShips"]
[[3,3],[0,0,2,"H"],[0,1,2,"V"],[1,1,2,"H"],[2,2],[1,1],[]]
Output:
[null,0,-1,1,"miss","hit",2]
```
Ship 0 covers (0,0) and (0,1). The vertical ship starting at (0,1) would cover (0,1) and (1,1), and (0,1) is taken, so that placement returns -1. The next valid ship gets id 1 and covers (1,1) and (1,2). The shot at (2,2) lands on an empty cell. The shot at (1,1) damages ship 1, which still has one cell left, so both ships remain.
Example 2:
```text
Input:
["BattleshipGame","placeShip","placeShip","placeShip","fireShot","fireShot","fireShot","fireShot","remainingShips","fireShot","remainingShips"]
[[2,3],[0,0,3,"H"],[1,2,2,"V"],[1,0,1,"H"],[1,0],[1,0],[0,0],[0,1],[],[0,2],[]]
Output:
[null,0,-1,1,"sink","miss","hit","hit",1,"win",0]
```
The vertical ship at (1,2) would need row 2, which is off a 2-row board. Ship 1 has a single cell, so the first shot at (1,0) sinks it while ship 0 is still afloat, and the repeated shot at (1,0) is a miss. Ship 0 takes two hits, and the shot at (0,2) sinks it as the last ship on the board.
```hint What a shot needs to know
When a shot lands, you want to learn in one lookup which ship (if any) is on that cell, and in one more step whether that ship just went down. Choose the board and per-ship state with that in mind.
```
```hint Failed placements
A placement can fail on its last cell. Make sure a rejected ship leaves nothing on the board and does not use up an id.
```
### Constraints & Assumptions
- `1 <= rows, cols <= 100`
- `1 <= length <= max(rows, cols)`
- `0 <= row < rows` and `0 <= col < cols`
- `orientation` is either `"H"` (horizontal, extending right) or `"V"` (vertical, extending down).
- At most 10,000 operations in total.
- Only successful placements receive an id, as example 1 shows.
- All ships are placed before the first shot.
### Clarifying Questions to Ask
- May ships touch along an edge or a corner, or is overlap the only forbidden case?
- What should `fireShot` return when no ships have been placed at all?
- Should `fireShot` also report which ship was hit or sunk, or is the result string enough?
- Is this one board for now, with two players and turns coming later, so the design should leave room for that?
### What a Strong Answer Covers
- The board representation and per-ship state that let a shot be resolved in constant time.
- Placement validation for bounds and overlap that checks every cell before changing any state.
- How repeated shots are detected so a ship is never damaged twice by the same cell.
- How a sink is told apart from a win, and the time and space cost of each method.
### Follow-up Questions
- How would you extend the design to two players, each with a private board, taking turns?
- How would you also enforce the classic rule that ships may not touch, even diagonally?
- How would you place a given fleet at random so that every placement is valid?
- What changes if the board is huge and sparse, for example a billion cells on each side?
Overview: Design a Battleship simulation on a rectangular grid that places ships horizontally or vertically and resolves each shot as a miss, hit, sink or win. It tests board and ship state modeling, placement validation for bounds and overlap, repeated-shot handling, and constant-time shot resolution.