Quick Overview

This question evaluates a candidate's ability to analyze grid-based data structures and identify connected components, testing spatial adjacency reasoning and state management within 2D arrays.

Count islands in a binary grid

Company: Samsung

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

## Problem You are given an `m x n` grid of characters where each cell is either `'1'` (land) or `'0'` (water). An **island** is formed by connecting adjacent land cells **horizontally or vertically** (not diagonally). The grid edges are surrounded by water. Return the **number of islands** in the grid. ## Input - `grid`: a 2D array/list of characters `'0'` and `'1'` ## Output - An integer: the number of islands ## Constraints - `1 <= m, n <= 300` - `grid[i][j] ∈ {'0','1'}` ## Example Input: ``` [ ['1','1','0','0','0'], ['1','1','0','0','0'], ['0','0','1','0','0'], ['0','0','0','1','1'] ] ``` Output: ``` 3 ``` Explanation: There are three disconnected land components.

Overview: This question evaluates a candidate's ability to analyze grid-based data structures and identify connected components, testing spatial adjacency reasoning and state management within 2D arrays.

Return the number of 4-directionally connected land components in a binary grid.

Examples

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

Expected Output: 3

Explanation: Diagonal land is separate.

Input: ([],)

Expected Output: 0

Explanation: Empty grid.

Count Islands in a Binary Grid

easySamsungSoftware Engineer

Return the number of 4-directionally connected land components in a binary grid.

View full question (all parts)

Problem

You are given an m x n grid of characters where each cell is either '1' (land) or '0' (water). An island is formed by connecting adjacent land cells horizontally or vertically (not diagonally). The grid edges are surrounded by water.

Return the number of islands in the grid.

Input

  • grid: a 2D array/list of characters '0' and '1'

Output

  • An integer: the number of islands

Constraints

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

Example

Input:

[
  ['1','1','0','0','0'],
  ['1','1','0','0','0'],
  ['0','0','1','0','0'],
  ['0','0','0','1','1']
]

Output:

3

Explanation: There are three disconnected land components.

Examples

Example 1
Input
([['1', '1', '0'], ['0', '1', '0'], ['1', '0', '1']],)
Output
3
Notes
Diagonal land is separate.
Example 2
Input
([],)
Output
0
Notes
Empty grid.

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[str]]) -> int:
Sample call
solution([['1', '1', '0'], ['0', '1', '0'], ['1', '0', '1']])
Language
Case 1
Input
([['1', '1', '0'], ['0', '1', '0'], ['1', '0', '1']],)
Expected
3
Case 2
Input
([],)
Expected
0

Browse More Questions