Problem · Array

Find Network Calls (Also For Amazon UK :)

Learn this problem
MediumAmazonFULLTIMEOA
See Amazon hiring insights

Problem statement

A digital commerce platform is conducting an experiment on the number of customer feedback entries associated with each item in its catalog.

You are provided with an array feedback of size n, where feedback[i] denotes the current number of feedback entries for the i-th item. The platform offers API endpoints that allow modifying these counts by either increasing or decreasing them by one per call.

Given an integer array targetCounts of size q, your task is to determine the number of API calls required to adjust the feedback count of every item in feedback so that all items match each value in targetCounts.

The objective is to return an array of size q, where the i-th element represents the total number of API calls necessary to align all feedback counts with targetCounts[i].

Function

findNetworkCalls(feedback: int[], targetCounts: int[]) → long[]

Complete the function findNetworkCalls in the editor below.

findNetworkCalls has the following parameters:

  1. int feedback[n]: the initial count of reviews of each product
  2. int targetCounts[q]: the equal count of reviews

Returns

long[]: an array where each element denotes the total API calls needed to align all feedback counts to the corresponding value in targetCounts

🌷 ᡣ𐭩જ⁀➴All Credit goes to interested_max ༊·˚

Examples

Example 1

feedback = [4, 6, 5, 2, 1]targetCounts = [3]return = [10, 20]
Example 1 illustration
Answer Version 1 - Therefore, the total API calls made to change the number of reviews for all products to 3 is: 1 + 3 + 2 + 1 + 2 = 9. Hence, return the array [9]. Answer Version 2 (newly found on 03-31-2025) - The source indicates that the expected output for this test case is expected to be [10, 20]. As we can see, the table is not complete in the source found this time. I will update once find more complete source. As the screenshots were taken on the platform, our expected output will go with [10, 20].

Example 2

feedback = [3, 6, 6]targetCounts = [5, 6]return = [4, 3]
For counts value 5 - The total number of API calls to be made is 2 + 1 + 1 = 4. For counts value 6 - The total number of API calls to be made is 3 + 0 + 0 = 3. So, we return [4, 3]. P.s. This new test was added on 03-31-2025. Relevant source image was included in the Problem Source section below.

Constraints

  • 1 ≤ n ≤ 10^5
  • 1 ≤ feedback[i] ≤ 10^6
  • 1 ≤ q ≤ 10^5
  • 1 ≤ targetCounts[i] ≤ 10^6
  • More Amazon problems

    drafts saved locally
    public long[] findNetworkCalls(int[] feedback, int[] targetCounts) {
      // write your code here
    }
    
    feedback[4, 6, 5, 2, 1]
    targetCounts[3]
    expected[10, 20]
    checking account