Find Servers That Handled Most Number of Requests - Problem
Load Balancer Challenge: You're managing a server farm with k servers numbered from 0 to k-1. Each server can handle only one request at a time but has infinite computational capacity.

When the i-th request arrives, your load balancer follows these rules:
1. If all servers are busy → drop the request
2. If server (i % k) is available → assign to that server
3. Otherwise → find the next available server in circular order

Given arrays arrival (request arrival times) and load (processing durations), find which server(s) handled the most requests.

Example: With 3 servers and requests arriving at times [1,2,3,4] with loads [5,2,3,3], server assignment follows the circular pattern while respecting busy periods.

Input & Output

example_1.py — Basic Load Balancing
$ Input: k = 3, arrival = [1,2,3,4,5], load = [5,2,3,3,3]
Output: [0,1,2]
💡 Note: Request 0 (arrival=1, load=5) → server 0 (preferred 0%3=0, available). Request 1 (arrival=2, load=2) → server 1 (preferred 1%3=1, available). Request 2 (arrival=3, load=3) → server 2 (preferred 2%3=2, available). Request 3 (arrival=4, load=3) → server 0 (preferred 3%3=0, but busy until time 6, so check server 1 busy until 4, so check server 2 busy until 6, so assign to server 1). Request 4 (arrival=5, load=3) → server 2. All servers handled 2 requests each.
example_2.py — Servers Get Busy
$ Input: k = 3, arrival = [1,2,3,4], load = [1,2,3,4]
Output: [0]
💡 Note: Request 0 → server 0 (free at time 2). Request 1 → server 1 (free at time 4). Request 2 → server 2 (free at time 6). Request 3 → server 0 (preferred server 0 is free at time 2, current time is 4). Server 0 handles 2 requests, others handle 1 each.
example_3.py — All Servers Busy
$ Input: k = 2, arrival = [1,2,3], load = [10,12,11]
Output: [0,1]
💡 Note: Request 0 → server 0 (busy until time 11). Request 1 → server 1 (busy until time 14). Request 2 at time 3 → all servers busy (0 until 11, 1 until 14), so request is dropped. Both servers handled 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 0Count: 2Free: 8Server 1Count: 2Free: 7Server 2Count: 2Free: 8Req 3Preferred busy→ Next availableAssignment Rules1. Try server (i % k)2. Search circularly3. Drop if all busyTimeline: Request ProcessingR0R1R2R3R4Time →
Understanding the Visualization
1
Request Arrives
New request arrives at specified time with given processing duration
2
Check Preferred Server
Calculate preferred server as request_index % k and check if available
3
Find Next Available
If preferred server busy, search circularly for next available server
4
Assign or Drop
Assign to found server or drop if all servers are busy
5
Update State
Update server's free time and increment its request counter
Key Takeaway
🎯 Key Insight: Use efficient data structures (min-heap + ordered set) to avoid O(k) linear searches, reducing complexity from O(n×k) to O(n×log k)
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
28.5K Views
Medium Frequency
~25 min Avg. Time
890 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