Problem · Hash Table

Count Server Replacements

Learn this problem
EasyAkuna Capital logoAkuna CapitalINTERNOA

Problem statement

You have n servers with IDs "s1", "s2", ..., "sn". The system processes a sequence of log entries, where each entry is formatted as "<server_id> <status>", and status is either "success" or "error".

For each server, track its consecutive errors:

  • If a server records three "error" logs in a row, it is considered faulty and is replaced. The replacement server keeps the same ID.
  • After a replacement, that server's consecutive error count resets to 0.
  • A "success" log also resets that server's consecutive error count to 0.

Determine the total number of server replacements that occur while processing all log entries.

Custom testing format

  1. The first line contains the integer n.
  2. The next line contains the integer m, the size of logs.
  3. Each of the next m lines contains one string element of logs.

Function

countFaults(n: int, logs: String[]) → int

Examples

Example 1

n = 2logs = ["s1 error", "s1 error", "s2 error", "s1 error", "s1 error", "s2 success"]return = 1
  1. Server s1 logs its first error: [error].
  2. Server s1 logs its second error: [error, error].
  3. Server s2 logs its first error: [error].
  4. Server s1 logs its third consecutive error: [error, error, error], so it is replaced.
  5. The new server s1 logs its first error: [error].
  6. Server s2 logs a success, so its consecutive-error record resets.

Only server s1 is replaced, and it is replaced once.

Constraints

  • 1 <= n <= 200
  • 1 <= logs.length <= 2 * 10^4

More Akuna Capital problems

drafts saved locally
public int countFaults(int n, String[] logs) {
  // write your code here
}
n2
logs["s1 error", "s1 error", "s2 error", "s1 error", "s1 error", "s2 success"]
expected1
checking account