← Questions

Server load scores from a day of requests

0
StripeCoding

A load balancer team wants a daily stress report for its server fleet. You get three inputs for one day:

  • servers: lines server_id,base_score. Every server starts at its integer base score.
  • requests: lines request_id,minute,server_id,client_id,payload_kb. minute is minutes since midnight (0 to 1439). Lines are not in time order.
  • rules: a map from request_id to its load rule: STANDARD, PRIORITY or BATCH. A request with no rule is STANDARD.

Skip malformed lines (wrong field count, empty ids, non-integer or out-of-range numbers) and requests for servers that are not in servers. Process the rest in time order. Break ties by input order.

Each request adds to its server's score in three independent categories:

  1. Volume stress. STANDARD adds 1. PRIORITY adds 2. BATCH adds ceil(payload_kb / 100), and at least 1.
  2. Repeat-client stress. Count this client's requests to this server with a minute in [t - 9, t], including the current request. If the count is more than 3, add 5.
  3. Peak-hour stress. A server's peak hour is the clock hour (minute // 60) with the most requests on that server. On a tie, use the earliest hour. Each request in that hour adds 2.

Implement compute_load_scores(servers, requests, rules) -> List[Tuple[str, int]]. Return every server with its final score, sorted by score descending, then by server id ascending.

Constraints

  • Up to 10^5 requests and 10^4 servers.

Example

servers  = ["web1,3", "web2,0"]
requests = ["q4,125,web1,ann,0", "q1,120,web1,ann,0", "q2,121,web1,ann,0",
            "q3,123,web1,ann,0", "q5,300,web2,ben,420", "q6,7,web9,ann,0"]
rules    = {"q5": "BATCH"}

web1: base 3 + volume 4 + repeat 5 (q4 is ann's 4th in 10 minutes)
      + peak 2*4 (hour 2 has 4 requests) = 20
web2: base 0 + volume 5 + peak 2*1 = 7
q6 is skipped (unknown server).

Result: [("web1", 20), ("web2", 7)]

Interviewers often add parts: make the weights configurable, take requests as a stream, and use the scores to pick a server for the next request.

Reconstructed from the reported question; details may differ from what interviewers ask.

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