You are a Backend Engineer at DoorDash and realized that your service can no longer keep up with traffic. In order to deal with the increased load, your team has decided to scale horizontally by adding more pods that run your service.
In order to achieve this, your colleague implemented a traffic router that distributes incoming traffic among pods using a round-robin algorithm (requests are routed to pods on a cyclical basis). Unfortunately, their implementation doesn’t quite seem to work, and you are tasked with debugging the code.
The implementation consists of the following classes:
TrafficRouter: Represents the Traffic Router. Its constructor takes a list of Backend pods as an argument.Pod: Represents a Pod running your service application. Its constructor takes hostname and port as arguments.Backend: Represents a Pod and its state ("AVAILABLE","UNAVAILABLE"). Its constructor takes a Pod and a state as arguments.HttpRequest: Represents an incoming Http Request. Its constructor takes an id as an argument. All requests from a user share the same request id.
And the following methods:
TrafficRouter.getBackend(request: HttpRequest): Returns the next available Pod that can serve the request using a round robin heuristic. If all Pods are unavailable, returnsnull.TrafficRouter.reportAvailability(backend: Backend, state: State): Marks a Backend Pod as either available or unavailable.TrafficRouter.addBackend(backend: Backend): Adds new backends to the rotation at runtime.
This problem is about building a stateful round-robin traffic router. The main idea is to maintain the backend order with a list or queue while tracking each pod’s availability; when a request arrives, the router scans for the next available backend in rotation and returns it, or null if none are available. The tricky parts are skipping unavailable backends, updating state correctly through reportAvailability, and incorporating newly added backends without breaking the rotation order. A clean implementation usually combines an indexed list, state tracking, and careful wrap-around logic.