Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Battleship Guessing Step
00:00
5 left

Battleship Guessing Step

MediumPython

Problem

Battleship game design: implement only the guessing step.

Given ship coordinates, previously guessed coordinates, and one new guess, classify the guess and return the updated guess history.

Input and Output

Implement guess_battleship(ships, guessed, row, col).

  • ships is a list of ships. Each ship is a list of [row, col] cells.
  • guessed is the list of [row, col] cells guessed before this turn.
  • Return a dictionary {"status": ..., "guessed": ...}.

Status values

Check these rules in order and return the first one that applies:

  1. already_guessed: [row, col] is already in guessed. Return guessed unchanged.
  2. miss: the cell is not part of any ship.
  3. win: the cell hits a ship, and after this guess every cell of every ship has been guessed.
  4. sunk: the cell hits a ship, and after this guess every cell of that ship has been guessed, but other ships still have unguessed cells.
  5. hit: the cell hits a ship that still has unguessed cells.

For every status except already_guessed, the returned guessed is the input list with [row, col] appended at the end.

Constraints

  • 1 <= len(ships)
  • 1 <= len(ships[i])
  • Ships do not overlap
  • All coordinates are integer pairs
  • The new guess is a valid coordinate pair
  • The input lists contain no malformed coordinates

Function Signature

def guess_battleship(ships, guessed, row, col):
Interviewer

Your question is Battleship Guessing Step. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.