← Back to companies
[ OK ] Loaded —
[ INFO ]
$ cd
$ ls -lt
01
02
03
04
05
$ ls -lt
01
02
03
04
05
user@intervues:~/$
In this problem you will build a simplified Battleship game step by step. You have access to an AI autocomplete tool — use it to speed up your implementation, but make sure the logic is correct.
Implement a Battleship class that manages a single-player game board. The board is an n x n grid where ships can be placed and shots can be fired.
(row, col) coordinates they occupy.'water', 'ship', 'hit', or 'miss'.Work through Parts A, B, and C in order. Each part builds on the previous one.
Implement the constructor and place_ship method:
__init__(self, n: int) — Create an n x n board. All cells start as 'water'.place_ship(self, coordinates: List[List[int]]) -> bool — Place a ship on the board. Each element is [row, col]. Return True if placement is valid (all cells are 'water' and within bounds), False otherwise. On success, mark those cells as 'ship'.Implement the take_shot method:
take_shot(self, row: int, col: int) -> str — Fire at (row, col). Returns:
'hit' if the cell is 'ship' (mark it 'hit')'miss' if the cell is 'water' (mark it 'miss')'already_taken' if the cell is 'hit' or 'miss''invalid' if out of boundsImplement helper methods:
get_board_state(self) -> List[List[str]] — Return the full board as a 2D list of cell states.all_ships_sunk(self) -> bool — Return True if no 'ship' cells remain on the board.shots_taken(self) -> int — Return total number of shots fired (hits + misses).