Take down servers in a reliability drill with minimum total requests
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked at Amazon. It tests greedy, sorting and scheduling.
A reliability drill shuts down a fleet of n servers. Server i handles request[i] requests every second while it is up, and starts with health[i] health. You are also given an integer k.
The drill runs in whole seconds. In each second:
- Every server that is still up receives its full load, so the drill sends
request[i]requests to each live serveri. - You inject one virus into a single live server of your choice, lowering its health by
k. A server whose health reaches 0 or below goes down and receives no more requests.
After the last server goes down, the drill sends one extra request to confirm the outage.
Implement min_total_requests(request: list[int], health: list[int], k: int) -> int that returns the minimum total number of requests sent over the whole drill, including the confirmation request.
Constraints
0 <= n <= 10^51 <= request[i], health[i] <= 10^91 <= k <= 10^9
Example 1
request = [1, 5, 2], health = [3, 2, 4], k = 2
output: 17
Server 1 needs one hit, servers 0 and 2 need two each. Take down server 1 (second 1), then server 2 (seconds 2-3), then server 0 (seconds 4-5). Requests: second 1 sends 1+5+2 = 8, seconds 2-3 send 1+2 = 3 each, seconds 4-5 send 1 each. Total 8 + 6 + 2 = 16, plus the confirmation request = 17.
Example 2
request = [7], health = [10], k = 3
output: 29
The server needs 4 hits, so it receives 7 requests in each of 4 seconds (28), plus 1 confirmation.
Clarifying questions
- Does a server that goes down in a second still receive that second's requests? (The example says yes: requests come first.)
- Is the confirmation request sent even when
n = 0, so the answer is 1? - Can the virus be injected into a server that has already gone down, wasting a second? (It never helps.)
- Can the total exceed 64 bits? Up to
10^5servers,10^9requests each, and up to10^9seconds each.
What a strong answer covers
- Computes each server's hits as
ceil(health[i] / k)and sees that it is best to finish servers one at a time, since splitting hits only keeps servers up longer. - Reduces the problem to ordering jobs to minimize the weighted sum of completion times: server
itakeshits[i]seconds and costsrequest[i]per second it is up. Sorting byhits[i] / request[i]ascending (Smith's rule) is optimal, proved by swapping two adjacent servers. - Compares the ratios with cross-multiplication to avoid floating point, then adds
request[i] * completion_timeover the order, plus 1. - O(n log n) time. Uses Python integers or 128-bit math, since the total can reach about
10^28. - Tests: both examples,
n = 0, equal ratios,klarger than every health (one hit each, so the largest request goes first), and a brute force over all orders for smalln.
Follow-ups
- Inject
vviruses per second instead of one. - Each server needs a different number of hits per virus, or viruses take time to act.
- Return the order of servers, not just the total.
- Servers depend on each other, so some must go down before others.
Where it's asked
What it tests
greedy · sorting · scheduling
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.
