Leetcode #1606: Find Servers That Handled Most Number of Requests
In this guide, we solve Leetcode #1606 Find Servers That Handled Most Number of Requests in Python and focus on the core idea that makes the solution efficient.
You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Problem Statement
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.
Quick Facts
- Difficulty: Hard
- Premium: No
- Tags: Array, Ordered Set, Simulation, Heap (Priority Queue)
Intuition
We need to repeatedly access the smallest or largest element as the input changes.
A heap provides fast insertions and removals while keeping order.
Approach
Push candidates into the heap as you scan, and pop when you need the best element.
Keep the heap size bounded if the problem requires a top-k structure.
Steps:
- Push candidates into a heap.
- Pop the best candidate when needed.
- Maintain heap size or invariants.
Example
Input: k = 3, arrival = [1,2,3,4,5], load = [5,2,3,3,3]
Output: [1]
Explanation:
All of the servers start out available.
The first 3 requests are handled by the first 3 servers in order.
Request 3 comes in. Server 0 is busy, so it's assigned to the next available server, which is 1.
Request 4 comes in. It cannot be handled since all servers are busy, so it is dropped.
Servers 0 and 2 handled one request each, while server 1 handled two requests. Hence server 1 is the busiest server.
Python Solution
class Solution:
def busiestServers(self, k: int, arrival: List[int], load: List[int]) -> List[int]:
free = SortedList(range(k))
busy = []
cnt = [0] * k
for i, (start, t) in enumerate(zip(arrival, load)):
while busy and busy[0][0] <= start:
free.add(busy[0][1])
heappop(busy)
if not free:
continue
j = free.bisect_left(i % k)
if j == len(free):
j = 0
server = free[j]
cnt[server] += 1
heappush(busy, (start + t, server))
free.remove(server)
mx = max(cnt)
return [i for i, v in enumerate(cnt) if v == mx]
Complexity
The time complexity is O(n log n). The space complexity is O(n).
Edge Cases and Pitfalls
Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.
Summary
This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.