Design the battleship game
PremiumMachine codingMedium
- Ship placement validation
- Hit and sunk tracking
- Hidden board rendering
Quick answer
This machine coding interview question asks you to design the battleship game. Aim for a design that meets its requirements at the stated scale, then explain its main components, data flow and trade-offs. Interviewers want to see how you scope the problem, size it and defend each choice.
The problem
Build the Battleship game as a two-player engine: each player hides five ships on a 10 × 10 grid and the players take turns firing at cells. The hard parts are placement validation, exact hit, miss and sunk results, and printing a board that hides the opponent's ships.
The round works like a take-home followed by a review call. The interviewer reads your code with you, then asks you to add a small feature live, so code that is easy to change matters as much as code that works.
What must the battleship game do?
- Place ships: place(player, ship, start, dir) puts a ship at a cell like B3, across (H) or down (V). It fails with OUT_OF_BOUNDS, OVERLAP or ALREADY_PLACED.
- Fire: fire(player, cell) returns MISS, HIT, SUNK with the ship name, or WIN when the last ship sinks.
How well must it do it?
- Easy to change: Board size, fleet and rules are configuration, so a live feature touches one or two classes.
- Constant-time shots: A shot finds its ship and updates sunk state in O(1), without scanning the board.
What scale should you design for?
Use these numbers in your estimates.
The scale numbers unlock with this questionUnlock
Key concepts
- Ship placement validation
- Hit and sunk tracking
- Hidden board rendering
- Alternating player moves
- Seeded random
- Unit testing
How you’ll practise
- Requirements
- Entities and interfaces
- Core implementation
- Edge cases and tests
- Extensions
- Wrap-up
Lala, personalized to you, gives a hint when you ask and reviews your design when you are ready.
What do interviewers look for?
In any machine coding design round.
- Clarifies the requirements before drawing anything
- Uses a few numbers that actually change the design
- Draws a clear path from the client to storage
- Picks storage and caching with reasons, not by habit
- Explains what breaks first under load and how to fix it
- States trade-offs openly instead of claiming a perfect design
Questions people ask about this interview question
- Which design type is this question?
- It is a machine coding question. You design the battleship game and explain your choices the way you would in an interview.
- Is this question asked at entry level?
- It is marked Medium. Entry-level engineers can practise it, but it expects you to reason about requirements and trade-offs, not only list components.
- What should I cover first?
- Start with the requirements: what the system must do, how well it must do it and which numbers change the design. Agree on scope with the interviewer before you design anything.
- Can I practise it for free?
- It is a premium question. Sign up and unlock it once for 10 credits; it then stays unlocked. When you finish, Lala, your personalized AI mentor, can review your design for 30 credits.
Related questions
Updated · Written by the SevyDevy team