Problem · Array

Count Failed Executions

Learn this problem
MediumAmazonOA
See Amazon hiring insights

Problem statement

Hello! This problem is identified as a duplcate of Count Inaccurate Results (This is a text button)

In an Amazon computing environment, there is a critical sequence of n processes that must be executed in a specific order for accurate results. The process order is represented by the array requiredSequence, where requiredSequence[i] (1 ≤ i ≤ n) denotes the process to be executed at the i-th position. Accurate results require the execution of all preceding processes.

Facing a challenge, an individual guided by the array actualSequence mistakenly runs the processes in a different order, where actualSequence[i] (1 ≤ i ≤ n) signifies the process executed at the i-th position. Both requiredSequence and actualSequence are permutations of size n.

Determine the count of processes that fail to produce accurate results due to the deviation from the specified order.

A permutation is a sequence of integers from 1 to n of length n containing each number exactly once, for example [3, 2, 1] is a permutation while [1, 2, 1] is not.

Function

countFailedExecutions(requiredSequence: int[], actualSequence: int[]) → int

Complete the function countFailedExecutions in the editor.

countFailedExecutions has the following parameters:

  1. int requiredSequence[n]: order in which the processes should be executed
  2. int actualSequence[n]: order in which the processes were actually executed

Returns

int: the count of processes that fail to produce accurate results due to the deviation from the specified order

Examples

Example 1

requiredSequence = [4, 2, 3, 5, 1, 6]actualSequence = [2, 3, 5, 1, 6, 4]return = 5
The only difference between the arrays requiredSequence and actualSequence is that process 4 is executed first after all other processes, while the order of the remaining processes remains unchanged. Additionally, processes 2, 3, 5, 1, and 6 rely on process 4 to produce accurate results. However, since process 4 is executed after these processes, none of them yield accurate results. Process 4, which does not depend on any other process for successful execution, still produces correct results. As a result, the number of processes yielding inaccurate results is 5.

Example 2

requiredSequence = [3, 2, 1]actualSequence = [3, 2, 1]return = 0
We see that the order of execution of processes in actualSequence matches that of requiredSequence. Therefore, every process gives accurate results. Hence, 0 processes give inaccurate results, and we return that.

Example 3

requiredSequence = [2, 3, 5, 1, 4]actualSequence = [5, 2, 3, 4, 1]return = 2
We see that 2 processes would give inaccurate results and these would be the process 5 and 4. Process 5 gives inaccurate results because in the original order, it needs processes 2 and 3 to run successfully, and the individual executes both of the mafter process 5. Also process 4 needs all the other processes to be executed before it runs properly. But the individual executes process 1 afterward, resulting in 4 giving inaccurate results. So, 2 processes give inaccurate results, and we return that.

Constraints

  • 1 ≤ n ≤ 2*10^5
  • 1 ≤ requiredSequence[i], actualSequence[i] ≤ n
  • requiredSequence and actualSequence are permutations of integers from 1 to n

More Amazon problems

drafts saved locally
public int countFailedExecutions(int[] requiredSequence, int[] actualSequence) {
  // write your code here
}
requiredSequence[4, 2, 3, 5, 1, 6]
actualSequence[2, 3, 5, 1, 6, 4]
expected5
checking account