Frontend School

Count Isolated Warehouse Items

DSA
easy

Count Isolated Warehouse Items

Easy
Matrix
Arrays
Grid

Overview

Zepto's success hinges on efficient warehouse management and precise inventory tracking. Our warehouses utilize a grid-based layout where each cell represents a storage slot. Identifying items that are unexpectedly isolated from others can signal potential misplacement or inefficiencies in storage. This problem asks you to analyze a warehouse grid to count such isolated items.

What you need to do / Task

Implement the function countIsolatedItems that takes a 2D array (matrix) representing a warehouse grid. Your task is to:

  1. Iterate through each cell of the grid.
  2. For every cell containing an item (represented by 1), determine if it is "isolated".
  3. An item is considered isolated if it does not have any other item (1) directly adjacent to it horizontally or vertically (i.e., its top, bottom, left, and right neighbors are either 0 or outside the grid boundaries).
  4. Return the total count of such isolated items.

Input & output

Input:

  • grid: A number[][] (2D array of numbers) representing the warehouse layout. Each element grid[i][j] will be either 0 (empty slot) or 1 (contains an item).

Output:

  • A number representing the total count of isolated items in the grid.

Clarifications

  • Grid Boundaries: Cells outside the grid boundaries are considered 'empty' for the purpose of checking isolation. An item at grid[0][0] only needs to check grid[0][1] (right) and grid[1][0] (bottom) to determine if it's isolated.
  • Empty Grid: The grid will not be empty; it will always have at least one row and one column.
  • Single-cell Grid: A 1x1 grid containing 1 will result in 1 isolated item. A 1x1 grid containing 0 will result in 0 isolated items.
  • All Zeros/Ones: If the grid contains only 0s, the result will be 0. If the grid contains only 1s, the result will be 0 (unless it's a 1x1 grid), as every item would have adjacent items.

Examples

1

Example 1

Input:

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

Output:

4

Note:

Items at (0,1), (1,0), (1,2), and (2,1) are all surrounded by zeros or boundaries, making them isolated. Total 4.

2

Example 2

Input:

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

Output:

1

Note:

Item at (0,0) is adjacent to (0,1). Item at (0,1) is adjacent to (0,0) and (1,1). Item at (1,1) is adjacent to (0,1). Only the item at (2,2) has no adjacent items (horizontally or vertically). Total 1.

3

Example 3

Input:

grid = [[1]]

Output:

1

Note:

The single item at (0,0) has no adjacent items, making it isolated. Total 1.

Constraints

Constraints & assumptions

  • 1 <= grid.length <= 100 (number of rows)
  • 1 <= grid[i].length <= 100 (number of columns, grid[i].length will be consistent for all rows)
  • grid[i][j] will be either 0 or 1.

What interviewers evaluate

  • Correctness: Does the solution accurately identify and count all isolated items, including edge cases like boundary conditions?
  • Efficiency: Is the solution optimally efficient in terms of time and space complexity (aim for O(rows * columns) time)?
  • Code Quality: Is the code clean, readable, well-structured, and easy to understand?
  • Edge Case Handling: Does the solution correctly handle grids of various sizes, including 1x1, and grids with all zeros or all ones?

Loading editor...