Design a Lead Distribution Engine
🎯 What is a lead distribution engine? A lead is a potential customer: someone who filled in a "contact sales" form or asked for a demo. A lead distribution engine decides which salesperson calls them back. Each team in the company, such as enterprise sales or small-business sales, wants its own rule for that: take turns, give it to whoever is least busy, or give it to someone who speaks the customer's language.
This problem looks like a CRM feature, but underneath it is the same design as a support-ticket router or a load balancer: a pool of workers, a stream of jobs, and a rule for matching them that changes from team to team. That makes it a clean test of the Strategy pattern, of keeping state in the right place, and of tie-breaking rules that are easy to get subtly wrong.
Requirements
"Design a system that distributes incoming leads to sales agents. The company has several business units, and each one wants a different way of assigning leads."
The prompt names the moving part (the assignment rule) but almost nothing else. The questions below pin down the rules we will code against.
Clarifying Questions
You: What does a business unit look like? Can an agent work for two units?
Interviewer: A unit is a team with its own agents. Each agent belongs to exactly one unit. The caller already knows which unit a lead is for.
You: Which assignment rules do the units want?
Interviewer: Three for now. Round robin, so everyone gets the same share. Least loaded, so the agent with the fewest open leads gets the next one. And skill match: a lead may need skills like "spanish" or "enterprise", and only agents with all of them qualify; among those, the least loaded wins. A unit can switch its rule at any time.
You: Is there a limit on how many leads an agent can hold?
Interviewer: Yes. Each agent has a capacity: the most open leads they may hold at once. A full agent gets nothing new until they close a lead.
You: What about agents who are on holiday or off shift?
Interviewer: Agents can be marked offline. Offline agents get no new leads, but they keep the ones they already have.
You: What happens when nobody in the unit can take a lead?
Interviewer: Report that it could not be assigned. The caller will retry later.
You: When two agents are equally good, who wins?
Interviewer: The one who joined the unit first.
You: Do many leads arrive at the same moment, from several threads?
Interviewer: Assume one thread for now. We can talk about concurrency at the end.
The fourth answer is easy to skip past, but it splits two ideas that many candidates mix up: who can take a lead (online, not full) and who should take it (the unit's rule). The first is the same for every unit. The second is the part that changes. Keeping them apart is the main design decision in this problem.
💡 Interview tip: Always ask how ties are broken. "Least loaded" with no tie rule gives a different answer depending on how a dictionary happens to be ordered, and your interviewer's test cases will not match yours. "Earliest to join" is simple, stable and easy to explain.
Final Requirements
- The system has business units. Each unit has agents and exactly one assignment strategy, which can be replaced at any time.
- An agent belongs to one unit and has an id, a capacity and a set of skills. Agents can be marked available or unavailable.
- An agent can take a lead only if they are available and have fewer open leads than their capacity.
- Strategies choose among the agents who can take the lead:
- Round robin: the next agent after the one who got the previous lead, wrapping around.
- Least loaded: the fewest open leads.
- Skill match: only agents with every skill the lead needs; among them, the fewest open leads.
- Ties go to the agent who joined the unit first.
- If no agent qualifies, the lead is not assigned and the caller is told so.
- A lead can be closed, which frees one unit of its agent's capacity. Anyone can ask how many open leads an agent has.
- New strategies will be added, so adding one must not change the rest of the system.
Out of scope
- Deciding which unit a lead belongs to (territory and routing rules).
- Lead scoring, deduplication of the same customer, and the sales pipeline after assignment.
- Notifying agents, persistence and a user interface.
- Several threads at once (we come back to this in Extensibility).