Practice/DoorDash/Round Robin Algorithm: Debugging and Testing
CodingMust
You're building a load balancer that distributes incoming requests across multiple backend servers. Your task is to implement the round robin algorithm, which cycles through available servers in sequential order.
Given a list of server identifiers and a number of incoming requests, return a list showing which server handles each request in order. The algorithm should:
Example 1:
Input: servers = ["web-1", "web-2", "web-3"], num_requests = 5 Output: ["web-1", "web-2", "web-3", "web-1", "web-2"] Explanation: The first request goes to web-1, second to web-2, third to web-3, then we wrap around: fourth to web-1, fifth to web-2.
Example 2:
Input: servers = ["api-server"], num_requests = 3 Output: ["api-server", "api-server", "api-server"] Explanation: With only one server, all requests are handled by it.
Example 3:
Input: servers = ["db-1", "db-2"], num_requests = 0 Output: [] Explanation: No requests means an empty result list.