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

Number of Islands in Grid

EasyPython

Problem

A Supermicro SuperBlade monitoring panel represents active components as a 2D grid. Each cell contains "1" for an active component and "0" for an inactive cell. Active cells connected horizontally or vertically belong to the same zone.

Implement num_islands(grid) to return the number of distinct active zones. You may modify grid while processing it.

Formal Specification

  • Input: grid, a list of m rows, where each row is a list of strings containing only "0" and "1". The grid may be empty.
  • Output: An integer representing the number of connected groups of "1" cells.
  • Diagonal cells are not considered connected.

Constraints

  • 0 <= m <= 300
  • 0 <= n <= 300
  • Each row contains exactly n cells
  • Each cell is either "0" or "1"
  • The total number of cells is at most 90,000

Function Signature

def num_islands(grid):
Interviewer

Your question is Number of Islands in Grid. 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.