Frontend School

Count Isolated Binary Cells

DSA
easy

Count Isolated Binary Cells

Easy
Matrix
Arrays
Traversal

Overview

Imagine you are analyzing sensor data from a grid-based monitoring system, where each cell represents a specific area and contains either a 0 or a 1. A 1 indicates an active signal, and a 0 indicates no signal. For certain anomaly detection tasks, it's crucial to identify "isolated" active signals – those that are not connected to any other active signals. This problem requires you to implement a function that counts such isolated active signals in a given binary grid.

What you need to do / Task

You are given a 2D binary matrix (grid) representing sensor data. Your task is to implement a function countIsolatedCells that returns the total number of "isolated" cells in the grid. An active cell (a cell containing 1) is considered "isolated" if all its immediate orthogonal neighbors (up, down, left, right) are 0s.

  • If a cell is on the boundary of the grid, it will have fewer immediate neighbors. You only need to check the valid neighbors within the grid boundaries.
  • Only cells containing 1 can be considered isolated. Cells containing 0 are never isolated.

Input & output

The function countIsolatedCells takes one argument:

  • grid: A 2D array of numbers (number[][]) representing the binary matrix. Each element will be either 0 or 1.

The function should return:

  • An integer, representing the total count of isolated cells.

Clarifications

  • An empty grid ([] or [[]]) should return 0.
  • The grid will only contain 0s and 1s.
  • A cell (r, c) at the edge of the grid only considers its available neighbors. For example, grid[0][0] only has grid[0][1] (right) and grid[1][0] (down) as potential neighbors to check. If all such valid neighbors are 0s (or there are no valid neighbors), the cell is isolated.

Examples

1

Example 1

Input:

grid = [[1,0,1],[0,0,0],[1,0,1]]

Output:

4

Note:

Cells at (0,0), (0,2), (2,0), and (2,2) are all '1's and have only '0's as immediate neighbors. For example, (0,0) has neighbors (0,1) and (1,0), both of which are '0'.

2

Example 2

Input:

grid = [[1,1,1],[1,0,1],[1,1,1]]

Output:

0

Note:

Every '1' cell has at least one '1' as an immediate neighbor, so none are isolated. For instance, (0,0) has (0,1) and (1,0) as '1' neighbors.

3

Example 3

Input:

grid = [[0,0,0],[0,1,0],[0,0,0]]

Output:

1

Note:

Only the cell at (1,1) is a '1' and all its immediate orthogonal neighbors (0,1), (2,1), (1,0), (1,2) are '0's.

Constraints

Constraints & assumptions

  • 1 <= grid.length <= 100 (number of rows)
  • 1 <= grid[i].length <= 100 (number of columns for each row i)
  • grid[i][j] is 0 or 1.
  • The grid is rectangular (i.e., grid[i].length is the same for all i).

What interviewers evaluate

  • Correctness of the algorithm for identifying isolated cells.
  • Efficient iteration through the matrix.
  • Proper handling of boundary conditions for cells at the edges.
  • Clarity and readability of the code.

Loading editor...