# The Traffic Director

> Spread the load evenly - nobody should be doing all the work.

Canonical URL: <https://datadriven.io/problems/the_traffic_director>

Domain: Python · Difficulty: easy · Seniority: L4

## Problem

Given a list of server names and a list of requests, assign requests to servers in round-robin order (request i goes to servers[i % len(servers)]). Return a dict mapping each request to its assigned server.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **modular arithmetic** applied to round-robin distribution. It probes whether a candidate can use the modulo operator to cycle through a server pool, a fundamental load-balancing pattern.

---

### Break down the requirements

#### Step 1: Iterate through requests with an index

Each request's index determines which server it maps to.

#### Step 2: Assign using modulo

Server index is `i % len(servers)`, cycling through the pool.

#### Step 3: Build and return the assignment dict

Map each request identifier to its assigned server name.

---

### The solution

**Modulo-based cyclic assignment**

```python
def round_robin(servers: list, requests: list) -> dict:
    assignments = {}
    for i in range(len(requests)):
        server_idx = i % len(servers)
        assignments[requests[i]] = servers[server_idx]
    return assignments
```

> **Time and Space Complexity**
>
> **Time:** O(r) where r is the number of requests.
> 
> **Space:** O(r) for the assignments dict.

> **Interviewers Watch For**
>
> Handling the edge case where the server list is empty. Dividing by zero would crash. Strong candidates add a guard or mention it.

> **Common Pitfall**
>
> Manually tracking a counter and resetting it. The modulo operator handles wrapping automatically and is cleaner.

---

## Common follow-up questions

- What if servers had different capacities (weighted round-robin)? _(Tests expanding the server list proportionally or using a weighted counter.)_
- How would you handle server failures mid-stream? _(Tests dynamically removing servers from the pool and adjusting the rotation.)_
- What are alternatives to round-robin for load balancing? _(Tests knowledge of least-connections, consistent hashing, and random selection.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_traffic_director)
- [Python Interview Questions](https://datadriven.io/python-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.