Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Battleship Probe Challenge

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Battleship Probe Challenge. Start with the requirements on the right.

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.

Problem

You are given a square grid of size N (N>=3); using a probe API find a 1x3 or 3x1 ship on the grid (battleship variant)

Asked in the phone screen, coding stage. Treat grid[row][col] as the result of probing that cell, where 1 means occupied and 0 means empty. Return the three coordinates of the first ship found in row-major search order, checking horizontal ships before vertical ships for each scan, or return [] if none exists.

Input and Output

find_ship(n, grid) receives an integer n and an n x n binary list. Return a list of three [row, column] coordinate lists.

Constraints

  • n >= 3
  • grid has exactly n rows and n columns
  • Each grid value is either 0 or 1
  • A valid ship consists of exactly three consecutive cells horizontally or vertically
  • If multiple ships exist, return the first one according to the specified scan order

Function Signature

def find_ship(n, grid):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output