FastPrepFastPrep
Problem Brief

Get Total Requests

FULLTIMEOA
See Amazon online assessment and hiring insights

Developers at Amazon have their applications deployed on n servers. Initially, the ith server has an id server[i] and can handle server[i] requests at a time.

For maintenance purposes, some servers are replaced periodically. On a jth day, all the servers with id equal to replaced[j] are replaced with servers with id newId[j] that can serve newId[j] requests. The total number of requests served on a jth day is the sum of the ids of the servers that the application is running on.

Given server, replaced, and newId, find the total number of requests served by the servers each day.

Note: The indices i and j are assumed to follow 1-based indexing.

Function Description

Complete the function getTotalRequests in the editor.

getTotalRequests takes the following arguments:

  1. 1. int server[n]: the initial server ids
  2. 2. int replaced[n]: the ids of the servers to replace
  3. 3. int newId[n]: the new ids of the replaced servers

Returns

int[]: an array of integers representing the total number of requests served each day

1Example 1

Input
server = [20, 10], replaced = [10, 20], newId = [20, 1]
Output
[40, 2]
Explanation
Example 1 illustration

Day 1: The servers are [20, 10]. Server with id 10 is replaced by a server with id 20. New servers are [20, 20]. Total requests = 20 + 20 = 40.

Day 2: The servers are [20, 20]. Server with id 20 is replaced by a server with id 1. New servers are [1, 1]. Total requests = 1 + 1 = 2.

Hence the answer is [40, 2].

2Example 2

Input
server = [3, 3], replaced = [3, 1], newId = [1, 5]
Output
[2, 10]
Explanation
After the first day, the servers are [1, 1]. After the second day, the servers are [5, 5] :)

3Example 3

Input
server = [2, 5, 2], replaced = [2, 5, 3], newId = [3, 1, 5]
Output
[11, 7, 11]
Explanation
After the first day, the servers are [3, 5, 3].) After the second day, the servers are [3, 1, 3] :) After the third day,the servers are [5, 1, 5] :)

Constraints

Limits and guarantees your solution can rely on.

  • 1 <= n <= 105
  • 1 <= server[i], replacedId[i], newID[i] <= 104
  • public int[] getTotalRequests(int[] server, int[] replaced, int[] newId) {
      // write your code here
    }
    
    Input

    server

    [20, 10]

    replaced

    [10, 20]

    newId

    [20, 1]

    Output

    [40, 2]

    Sign in to submit your solution.