Design Game Matchmaking
🎮 What is a matchmaker? It is the part of an online game that decides who plays with whom. A player presses "Play", picks a mode such as 2v2, and the matchmaker finds other players of a similar level in the same region. As soon as enough players are together, it starts the game and splits them into two fair teams.
Matchmaking looks like a queue problem, but most of the interview is about rules: who is allowed in the same game, which open game a new player joins, when a game starts, and how to make the teams fair. Prompts often say "join or create a game": that is the heart of the design. A new player either joins an open lobby that fits them or becomes the first player of a new one. Level, mode and region are the three filters that decide "fits".
Requirements
"Design a matchmaking system for an online game. Players pick a mode like 1v1, 2v2 or 4v4 and get matched with players of a similar level in their region."
The prompt leaves almost every rule open. These are the questions that change the code.
Clarifying Questions
You: What does "similar level" mean exactly?
Interviewer: Levels go from 1 to 100. We configure a level range, say 10. Any two players in the same game must be at most that far apart.
You: Can a player be matched with someone from another region, or in another mode?
Interviewer: No. Mode and region must match exactly. A 2v2 player in Europe only plays 2v2 games in Europe.
You: When a player fits several open lobbies, which one do they join?
Interviewer: The oldest one. Those players have waited longest. If nothing fits, start a new lobby with this player in it.
You: When does a game start?
Interviewer: As soon as it is full: 2 players for 1v1, 4 for 2v2, 8 for 4v4. Once it starts, nobody can join or leave it.
You: How are the teams made?
Interviewer: Make them fair. Strong players should be spread across both teams, not all on one side.
You: Can players cancel while they wait? And can someone queue twice?
Interviewer: They can leave a lobby any time before the game starts. One player can only be in one lobby or game at a time. After a game ends, they can queue again.
You: Should matching widen over time if a player waits too long?
Interviewer: Not for now. Keep the range fixed. We may come back to it.
The answer about "the oldest lobby" is the one people skip. Without it, two correct-looking implementations put the same player in different games, and you cannot test either.
💡 Interview tip: "Fair teams" is vague on purpose. Offer a concrete rule ("sort by level, then snake draft") and check it with the interviewer. Turning a fuzzy word into a rule you can test is a big part of what is graded.
Final Requirements
- A player joins with an id, a level (1 to 100), a mode (
1v1,2v2,4v4) and a region. - Two players can be in the same game only if they have the same mode and region and their levels differ by at most the configured level range.
- A joining player goes into the oldest open lobby they fit. If none fits, a new lobby is created for them.
- A game starts as soon as it is full (2, 4 or 8 players). Started games accept no joins or leaves.
- When a game starts, its players are split into two teams with balanced total level.
- A player can leave their lobby before it starts. A lobby whose last player leaves is cancelled.
- A player is in at most one lobby or game at a time. Ending a game frees its players to queue again.
- Anyone can ask for a game's status and, once it has started, its teams.
Out of scope
- Networking, game servers and the game itself.
- Skill rating updates after a match (Elo and similar).
- Parties of friends queuing together, and widening the range over time (both in Extensibility).
- Many matchmaker threads at once (also in Extensibility).