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 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.
Reconstructed from the reported question; details may differ from what interviewers ask.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
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 see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
Sign in to submit your code; every attempt is kept here.
