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]
💡 Note: Server 0 gets requests 0 and 2 (2 requests), Server 1 gets request 1 (1 request). Server 0 handles the most requests.

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
Find Servers That Handled Most Requests INPUT k = 3 servers S0 S1 S2 arrival[] 1 2 3 4 5 load[] 5 2 3 3 3 indices: 0 1 2 3 4 Assignment: i % k Try server (i%k) first, then next available Requests arrive at t=1,2,3,4,5 with processing times ALGORITHM STEPS 1 Use Min-Heap + TreeSet Heap: busy servers by end time TreeSet: available servers 2 Process Each Request Free servers with end_time <= current arrival time 3 Find Available Server ceiling(i%k) in TreeSet or wrap to first available 4 Track Request Counts Increment count[server] Find max at end Request Processing: Req i%k Srv End 0 0 S0 t=6 1 1 S1 t=4 2 2 S2 t=6 3 0 S1 t=7 4 1 S1 t=8 FINAL RESULT Request Counts Per Server S0 1 S1 3 S2 1 Server 1 handled most requests! Output: [1] S1 handled 3 requests S0, S2 handled 1 each OK - Verified Key Insight: Use a Min-Heap to track busy servers by their end times, and a TreeSet to efficiently find the next available server using ceiling operation. Time complexity: O(n log k) where n is number of requests. The ceiling operation finds server >= (i%k), with wrap-around if needed. TutorialsPoint - Find Servers That Handled Most Number of Requests | Optimal Solution
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