Problem · Simulation

Count Faults

Learn this problem
EasyAmazonOA
See Amazon hiring insights

Problem statement

Implement a prototype service to automate the detection and replacement of faulty servers to improve the availabity of an application.

There are n servers with its s1, s2,...,sn, and an array of strings, logs, of size m. Log format is "<server_id> <success/error>", the id of the server and the status of the processed request. If a perticular server id logs an error for three consecutive requests, it is considered faulty and is replaced with a new server with the same id.

Given n and athe array logs, find the number of times a faulty server was replace.

Function

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

Complete the function countFaults in the editor 👉.

countFaults has the following parameter:

  • int n: the number of servers
  • String logs[m]: the application logs
  • Returns

    int: the number of times servers were replaced

    Examples

    Example 1

    n = 2logs = ["s1 error", "s1 error", "s2 error", "s1 error", "s1 error", "s2 success"]return = 1
    Example 1 illustration
    s1 was replaced one time. So output should be 1.

    Constraints

  • 1 <= n <= 200
  • 1 <= m <= 2 * 104 (The source image is too blurry. It looked like 104 to me. If you find it incorrect, please let me know! Many thanks in advance 💛💚🧡💖)
  • More Amazon 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