Find Servers That Handled Most Number of Requests - Problem

You have k servers numbered from 0 to k-1 that are being used to handle multiple requests simultaneously. Each server has infinite computational capacity but cannot handle more than one request at a time.

The requests are assigned to servers according to a specific algorithm:

  • The ith (0-indexed) request arrives.
  • If all servers are busy, the request is dropped (not handled at all).
  • If the (i % k)th server is available, assign the request to that server.
  • Otherwise, assign the request to the next available server (wrapping around the list of servers and starting from 0 if necessary).

You are given a strictly increasing array arrival of positive integers, where arrival[i] represents the arrival time of the ith request, and another array load, where load[i] represents the load of the ith request (the time it takes to complete).

Your goal is to find the busiest server(s). A server is considered busiest if it handled the most number of requests successfully among all the servers.

Return a list containing the IDs (0-indexed) of the busiest server(s). You may return the IDs in any order.

Input & Output

Example 1 — Basic Assignment
$ Input: k = 3, arrival = [1,2,3,4,5], load = [5,2,3,3,3]
Output: [1]
💡 Note: Server 0 gets requests 0,3 (2 requests), Server 1 gets requests 1,4 (2 requests), Server 2 gets request 2 (1 request). All tie with 2 requests each, but Server 1 handled most efficiently.
Example 2 — Request Dropping
$ Input: k = 3, arrival = [1,2,3,4], load = [1,2,1,2]
Output: [0]
💡 Note: Server 0 handles request 0 (finishes at time 2), Server 1 handles request 1 (finishes at time 4), Server 2 handles request 2 (finishes at time 4). Request 3 arrives at time 4 when all servers are busy, so it's dropped.
Example 3 — Wrapping Around
$ Input: k = 2, arrival = [1,2,3], load = [2,1,1]
Output: [0,1]
💡 Note: Server 0 gets request 0, Server 1 gets request 1. Request 2 prefers server 0 but it's busy, so wraps to server 1. Both servers handle 1 request each.

Constraints

  • 1 ≤ k ≤ 105
  • 1 ≤ arrival.length, load.length ≤ 105
  • arrival.length == load.length
  • 1 ≤ arrival[i], load[i] ≤ 109
  • arrival is strictly increasing.

Visualization

Tap to expand
Server Request Assignment: Find Busiest ServersInputk=3, arrival=[1,2,3,4,5]load=[5,2,3,3,3]Server 0Server 1Server 2Req: 0,3Req: 1,4Req: 2Assignment: Req 0→S0, Req 1→S1, Req 2→S2, Req 3→S0, Req 4→S1OutputServers 0,1 tie with 2 requestsReturn: [0,1]🎯 Key Insight: Use heaps to efficiently track server completion times
Understanding the Visualization
1
Input
k=3 servers, requests arrive at [1,2,3,4,5] with loads [5,2,3,3,3]
2
Process
Assign requests using rotation and availability rules
3
Output
Return servers with maximum request count
Key Takeaway
🎯 Key Insight: Use priority queues to efficiently manage server availability and avoid O(k) linear searches per request
Asked in
Amazon 15 Google 12 Microsoft 8 Facebook 6
23.4K Views
Medium Frequency
~35 min Avg. Time
567 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen