Problem · Array

Count Invalid Log Groups

Learn this problem
EasyIBM logoIBMNEW GRADOA
See IBM hiring insights

Problem statement

You are given logGroups, a list of log groups. Each group is an ordered list of status strings, and every status is either "UP" or "DOWN".

A group is valid only when both rules hold:

  • Its first status is "DOWN".
  • Every pair of adjacent statuses is different, so the statuses alternate between "DOWN" and "UP".

An empty group is invalid because it has no first "DOWN" status. A one-status group is valid exactly when that status is "DOWN". If logGroups is empty, return 0.

Return the number of invalid groups. Implement countInvalidLogGroups(List<List<String>> logGroups).

Function

countInvalidLogGroups(logGroups: List<List<String>>) → int

Examples

Example 1

logGroups = [["DOWN","UP","DOWN"],["UP","DOWN"],["DOWN","DOWN","UP"],["DOWN"]]return = 2

The second group is invalid because it starts with "UP". The third group is invalid because its first two adjacent statuses are both "DOWN". The first and fourth groups are valid, so the result is 2.

Example 2

logGroups = [[],["DOWN","UP"],["UP"]]return = 2

The empty group is invalid, the alternating two-status group is valid, and the singleton "UP" group is invalid. Therefore the answer is 2.

Example 3

logGroups = []return = 0

There are no groups to classify, so there are no invalid groups.

Constraints

  • 0 <= logGroups.length <= 10^5.
  • 0 <= logGroups[i].length.
  • The total number of statuses across all groups is at most 2 * 10^5.
  • Every status is exactly "UP" or "DOWN".

More IBM problems

drafts saved locally
public int countInvalidLogGroups(List<List<String>> logGroups) {
  // Write your code here.
}
logGroups[["DOWN","UP","DOWN"],["UP","DOWN"],["DOWN","DOWN","UP"],["DOWN"]]
expected2
checking account