Frontend School

Count Healthy Service Instances

DSA
easy

Count Healthy Service Instances

Easy
Matrix
Arrays

Overview

ServiceNow platforms often feature dashboards that provide a real-time overview of critical system health and performance. A common visualization component is a grid or matrix representing various service instances, where each cell indicates the current operational status of a specific instance. To quickly gauge the overall health of a system or a particular service cluster, it's essential to count how many of these instances are currently running without issues.

This problem simulates a simplified version of such a health monitoring scenario. You are tasked with processing a health grid and determining the total number of healthy service instances.

What you need to do / Task

Implement a function countHealthyServiceInstances that takes a 2D array (matrix) of integers, healthGrid, as input. Each element in the grid will be either 0 or 1, where 1 signifies a 'healthy' service instance and 0 signifies an 'unhealthy' instance. Your function should return the total count of healthy service instances across the entire grid.

Input & output

  • healthGrid: A number[][] (or int[][] in other languages) representing the health status of various service instances. Each inner array represents a row, and elements within it represent columns. Each element healthGrid[r][c] will be either 0 or 1.
  • Return: A number (or int) representing the total count of 1s found in the healthGrid.

Clarifications

  • Empty or malformed grids: You can assume that healthGrid will always contain at least one row, and each row will contain at least one column. All rows will have the same length, forming a valid rectangular matrix.
  • Element values: All elements in healthGrid are guaranteed to be either 0 or 1.

Examples

1

Example 1

Input:

healthGrid = [[0,1,0],[1,1,1],[0,0,1]]

Output:

5

Note:

There are five '1's in the grid: healthGrid[0][1], healthGrid[1][0], healthGrid[1][1], healthGrid[1][2], and healthGrid[2][2].

2

Example 2

Input:

healthGrid = [[0,0],[0,0]]

Output:

0

Note:

There are no '1's in the grid, so the count of healthy instances is 0.

3

Example 3

Input:

healthGrid = [[1]]

Output:

1

Note:

The grid contains a single healthy service instance.

Constraints

Constraints & assumptions

  • 1 <= healthGrid.length <= 100 (number of rows)
  • 1 <= healthGrid[i].length <= 100 (number of columns)
  • healthGrid[i][j] will always be 0 or 1.

Out of scope

  • Error handling for invalid input types (e.g., non-numeric values, values other than 0 or 1).
  • Performance optimization beyond a simple linear scan of the matrix, as the constraints are small.

What interviewers evaluate

  • Correctness in iterating through all elements of a 2D array.
  • Ability to accurately count specific values within the matrix.
  • Code readability and adherence to basic programming practices.
  • Understanding of fundamental time and space complexity for matrix traversal.

Loading editor...