Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Count Islands in a Grid

Medium
CodingMatrixGraphs
Asked 2mo ago|
Navi
Navi
Asked 2 times

Problem

Given a 2D grid of '1' and '0', count how many islands it contains. An island is formed by horizontally or vertically adjacent land cells, and water separates different islands.

Constraints

  • 1 <= rows, cols <= 300
  • grid[i][j] is '0' or '1'
  • Only 4-directional adjacency counts

You are practicing as a guest. Sign up free to run your code against the sample data. Your draft stays right here.

Sign up freeI have an account
def solve(rows):
    counts = {}
    for row in rows:
        ...
    return result
Sign up to unlock solutions
Navi Software Engineer Interview QuestionsNavi Interview Questions
Next questions
MetaCount Islands in Messenger GridHardPlaylistCount Islands in GridMediumZulilyCount Islands in GridMedium
Python 3.10