Quick Overview

This question evaluates understanding of grid traversal and graph connectivity concepts, focusing on modeling a 2D matrix as adjacency relationships within the coding and algorithms domain.

Count connected land components in a grid

Company: LinkedIn

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

You are given a 2D grid of characters where: - `'1'` represents land - `'0'` represents water A group of land cells forms an **island** if they are connected **4-directionally** (up, down, left, right). Determine the number of islands in the grid. ## Input - `grid`: an `m x n` matrix of characters (`'0'` or `'1'`). ## Output - Return an integer: the number of islands. ## Constraints - `1 <= m, n <= 300` - `grid[i][j] ∈ {'0','1'}` ## Notes - Diagonal adjacency does **not** connect islands. - You may modify the grid in-place if desired.

Overview: This question evaluates understanding of grid traversal and graph connectivity concepts, focusing on modeling a 2D matrix as adjacency relationships within the coding and algorithms domain.

Given a 1/0 grid, return the number of four-directionally connected land regions.

Constraints

  • Inputs are provided as Python literals compatible with the function signature.
  • Return a deterministic value exactly matching the requested output.

Examples

Input: ([['1','1','0','0'], ['1','0','0','1'], ['0','0','1','1']],)

Expected Output: 2

Explanation: Two regions.

Input: ([[1, 1], [1, 1]],)

Expected Output: 1

Explanation: One island.

Hints

  1. Start with a direct data structure representation.
  2. Handle edge cases before the main loop.

Count Islands

mediumLinkedInSoftware Engineer

Given a 1/0 grid, return the number of four-directionally connected land regions.

View full question (all parts)

You are given a 2D grid of characters where:

  • '1' represents land
  • '0' represents water

A group of land cells forms an island if they are connected 4-directionally (up, down, left, right). Determine the number of islands in the grid.

Input

  • grid: an m x n matrix of characters ('0' or '1').

Output

  • Return an integer: the number of islands.

Constraints

  • 1 <= m, n <= 300
  • grid[i][j] ∈ {'0','1'}

Notes

  • Diagonal adjacency does not connect islands.
  • You may modify the grid in-place if desired.

Examples

Example 1
Input
([['1','1','0','0'], ['1','0','0','1'], ['0','0','1','1']],)
Output
2
Notes
Two regions.
Example 2
Input
([[1, 1], [1, 1]],)
Output
1
Notes
One island.

Constraints

  • •Inputs are provided as Python literals compatible with the function signature.
  • •Return a deterministic value exactly matching the requested output.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def solution(grid: list[list[int]]) -> int:
Sample call
solution([['1','1','0','0'], ['1','0','0','1'], ['0','0','1','1']])
Language
Case 1
Input
([['1','1','0','0'], ['1','0','0','1'], ['0','0','1','1']],)
Expected
2
Case 2
Input
([[1, 1], [1, 1]],)
Expected
1

Browse More Questions