Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Count Islands in a Grid

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

Your question is Count Islands in a Grid. 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

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
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output