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

Number of Islands

MediumPython

Problem

https://leetcode.com/problems/number-of-islands/description/

Asked in the DSA stage. One of 3 DSA coding problems asked in this round.

Task

Given a 2D grid containing "1" for land and "0" for water, return the number of islands. An island is a group of horizontally or vertically adjacent land cells surrounded by water or the grid boundary.

Implement num_islands(grid). You may modify grid while traversing it.

Input and Output

grid is a list of nonempty lists of strings with equal lengths. Return an integer.

Constraints

  • 1 <= rows, columns <= 300
  • Each grid cell is either "0" or "1"
  • Only horizontal and vertical adjacency connects land cells

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.