Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Number of Islands
00:00
5 left

Number of Islands

MediumPython

Problem

How would you solve the number of islands problem in a 2D grid?

Implement num_islands(grid), where grid is a rectangular list of lists containing only the strings '1' and '0'. Land cells are connected only horizontally or vertically, not diagonally. Return the number of distinct islands without changing the input grid.

Constraints

  • 0 <= len(grid) <= 300
  • If grid is non-empty, every row has the same positive length
  • Each cell is either '0' or '1'
  • Connectivity is horizontal or vertical only
  • The input grid must not be modified

Function Signature

def num_islands(grid):
Interviewer

Your question is Number of Islands. 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
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.