Get Total Requests
Updated
Problem statement
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
getTotalRequests(server: int[], replaced: int[], newId: int[]) → int[]
Complete the function getTotalRequests in the editor.
getTotalRequests takes the following arguments:
- 1.
int server[n]: the initial server ids - 2.
int replaced[n]: the ids of the servers to replace - 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
Examples
Example 1
server = [20, 10]replaced = [10, 20]newId = [20, 1]return = [40, 2]
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].
Example 2
server = [3, 3]replaced = [3, 1]newId = [1, 5]return = [2, 10]After the first day, the servers are [1, 1]. After the second day, the servers are [5, 5] :)
Example 3
server = [2, 5, 2]replaced = [2, 5, 3]newId = [3, 1, 5]return = [11, 7, 11]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
1 ≤ n ≤ 10^51 ≤ server[i], replaced[i], newId[i] < 10^4server,replaced, andnewIdeach have exactlynelements.
More Amazon interview questions
- Add Course Batches All or NothingONSITE INTERVIEW · Seen Dec 2026
- Priority Ad Rotation with Show LimitsONSITE INTERVIEW · Seen Dec 2026
- Next Greater Element IONSITE INTERVIEW · Seen Dec 2026
- Count Connected ComponentsPHONE SCREEN · Seen Dec 2026
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Merge Accounts by Shared AttributePHONE SCREEN · Seen Dec 2026
- Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Minimum Grid InconvenienceOA · Seen Dec 2026
- Optimal Account BalancingONSITE INTERVIEW · Seen Dec 2026
- Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
Similar questions at other companies
- Macaron Order with Set DiscountsShopify · OAShopify interview questions
- Basic Item DiscountsPalantir · OAPalantir interview questions
- Calculate Portfolio Rebalancing DeltasAkuna Capital · OAAkuna Capital interview questions
- Filter Transaction Logs and Calculate Reliability MetricsStripe · OAStripe interview questions
- Balloon Color PairsInstacart · OAInstacart interview questions
- Command Frequency CounterUber · OAUber interview questions
- Cargo Port Ship StopsNutanix · OANutanix interview questions
- Bank Accounts with Balance HistoryMeta · OAMeta interview questions
- Parcel Attribute Store with TTLAirbnb · OAAirbnb interview questions
- Process Banking OperationsVercel · OA