Server load scores from a day of requests
I was asked this
No reports yet. Sign in to say where you were asked it.
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: linesserver_id,base_score. Every server starts at its integer base score. -
requests: linesrequest_id,minute,server_id,client_id,payload_kb.minuteis minutes since midnight (0 to 1439). Lines are not in time order. -
rules: a map fromrequest_idto its load rule:STANDARD,PRIORITYorBATCH. A request with no rule isSTANDARD.
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:
-
Volume stress.
STANDARDadds 1.PRIORITYadds 2.BATCHaddsceil(payload_kb / 100), and at least 1. -
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. -
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 belowt - 9before 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,BATCHwith 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
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.
