← Questions

Take down servers in a reliability drill with minimum total requests

0
AmazonCoding

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:

  1. Every server that is still up receives its full load, so the drill sends request[i] requests to each live server i.
  2. 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^5
  • 1 <= request[i], health[i] <= 10^9
  • 1 <= 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^5 servers, 10^9 requests each, and up to 10^9 seconds 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 i takes hits[i] seconds and costs request[i] per second it is up. Sorting by hits[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_time over 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, k larger than every health (one hit each, so the largest request goes first), and a brute force over all orders for small n.

Follow-ups

  • Inject v viruses 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

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding