Fleet capacity after server id replacements
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Amazon's online assessment, last asked Sep 2025. It tests hash map, simulation and counting.
A service runs on a fleet of n servers. Each server has an integer id, and a server with id x can handle x requests at a time. The fleet's total capacity is the sum of all server ids.
Servers are swapped out for maintenance, one batch per day. On day j, every server whose id is currently replaced[j] is replaced by a server with id new_id[j]. Servers that received a new id on an earlier day count under their new id. If no server has id replaced[j], nothing changes that day.
Implement:
def get_total_requests(server: list[int], replaced: list[int], new_id: list[int]) -> list[int]
Return a list whose j-th entry is the fleet's total capacity at the end of day j.
Constraints
-
1 <= n <= 10^5,0 <= q = len(replaced) = len(new_id) <= 10^5 1 <= server[i], replaced[j], new_id[j] <= 10^9- Totals can exceed 32 bits.
Example 1
server = [4, 4, 9]
replaced = [4, 9, 2]
new_id = [7, 2, 6]
output = [23, 16, 20]
Day 1: both 4s become 7, so the fleet is [7, 7, 9] = 23. Day 2: the 9 becomes 2, giving [7, 7, 2] = 16. Day 3: the 2 becomes 6, giving [7, 7, 6] = 20.
Example 2
server = [3, 3]
replaced = [3, 5]
new_id = [5, 1]
output = [10, 2]
The servers renamed to 5 on day 1 are replaced again on day 2.
An O(n * q) simulation is too slow at these limits. Aim for O(n + q).
Clarifying questions
- Can
new_id[j]equalreplaced[j](no change)? - Can
new_id[j]already exist in the fleet, so the two groups merge? - Is the output the total after each day, including days where nothing changed?
What a strong answer covers
- Keeps a hash map from id to the number of servers with that id, and the running total.
- On each day, takes the count
cforreplaced[j], removes it, addsctonew_id[j], and adjusts the total byc * (new_id[j] - replaced[j]): O(1) per day, O(n + q) overall. - Handles a missing id (nothing changes),
new_id == replaced, and merging into an existing id, which later days must replace together (Example 2). - Uses 64-bit or Python ints, since totals reach about 10^14.
- Tests both examples, a day with an absent id, and a chain of renames.
Follow-ups
- Return each original server's final id (union-find style relabelling).
- Replace only one server with that id per day, not all of them.
- Undo a day's replacement.
Where it's asked
- Amazon · Online assessment · Last asked Sep 2025Coding problems plus a repository bug-fix section with a built-in assistant or a design scenario, then a work-style survey · AI only inside the bug-fix section's built-in assistant
What it tests
hash map · simulation · counting
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
