FastPrepCount Faults

Count Faults

Amazon logoAmazonEasyOA
Learn

Problem statement

There are n servers with IDs s1, s2, ..., sn. You are given an array logs in chronological order. Each entry has the form "<server_id> <status>", where status is either success or error.

Track consecutive errors separately for each server. An entry for another server does not interrupt a server's streak. A success resets that server's streak to zero. Whenever a server reaches three consecutive errors, it is considered faulty and is immediately replaced by a new server with the same ID; after replacement, that ID's error streak also resets to zero.

Return the total number of server replacements recorded while processing all 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
Example 1 illustration

Server s1 logs errors on its first, second, and third requests. The intervening request for s2 does not break s1's streak, so s1 is replaced after its third error. Its following error starts a new streak. Server s2 never reaches three consecutive errors because its later success resets its streak. Therefore, exactly one replacement occurs.

Constraints

  • 1 <= n <= 200
  • 1 <= logs.length <= 2 * 10^4
  • Every log contains one of the server IDs s1 through sn followed by either success or error.

More Amazon problems

See Amazon hiring insights
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…