← Questions

Server load scores from a day of requests

0
StripeCoding

A coding interview question asked at Stripe. It tests simulation, sliding window and hash map.

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.

Clarifying questions

  • Is the repeat-client window counted over all of the client's requests to that server, or only over accepted (well-formed) ones?
  • Is the peak hour computed over all of the day's valid requests to the server, before any scoring? So a request early in the hour still gets the bonus.
  • What counts as out of range: a minute outside 0 to 1439, a negative payload?
  • Should a server with no requests still be listed at its base score?
  • Can a request's rule be unknown (not one of the three), and is that a malformed request or STANDARD?

What a strong answer covers

  • Parses and validates each line, keeps its input position, drops unknown servers, then sorts by (minute, position).
  • Volume stress is one lookup per request. Repeat-client stress keeps a deque of minutes per (client, server) pair and pops minutes below t - 9 before counting, so it is amortized O(1).
  • Peak-hour stress takes two passes: count requests per server and hour first, pick the earliest hour with the most requests, then add 2 for each request in it.
  • Sorts the results by (-score, server_id). Total O(R log R + S log S).
  • Tests: the example, unordered input, a tie on the peak hour, exactly 3 versus 4 requests in a 10-minute window, a window boundary at t - 9, BATCH with a payload of 0 and of 101, and each kind of malformed line.

Follow-ups

  • Make the weights and thresholds configurable.
  • Take requests as a stream and keep scores current; the peak hour then has to be revised as hours fill up.
  • Use the scores to pick a server for the next request.
  • Cover several days and a sliding 24-hour window.

Where it's asked

What it tests

simulation · sliding window · hash map · parsing

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