Implement a Matchmaker That Widens Skill and Region Criteria as Wait Time Grows
Company: Roblox
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a `Matchmaker` class that buckets players by skill level and region, and progressively widens the search criteria as a player's wait time increases.
Each player has a unique `name`, a `skill` rating, a `region` string, and an `enqueue_time` representing when they joined the queue. When searching for a match, the system determines a skill tolerance and whether cross-region matching is allowed based on how long the requesting player has been waiting (`current_time - enqueue_time`, in seconds).
The widening tiers are: a wait of less than 5 seconds allows a skill difference up to 100 (same region only), less than 15 seconds allows up to 300 (same region only), less than 30 seconds allows up to 500 (any region), and 30 seconds or more allows up to 1000 (any region). Among eligible candidates, the best match is the one with the smallest skill difference, breaking ties by earliest enqueue time, then lexicographic name order. A successful match removes both players from the queue.
Implement the following methods:
- `addPlayer(name: str, skill: int, region: str, enqueue_time: int) -> None`: Add a player to the matchmaking queue with the given name, skill rating, region, and enqueue time.
- `removePlayer(name: str) -> bool`: Remove a player from the queue. Returns `True` if the player was found and removed, `False` otherwise.
- `findMatch(name: str, current_time: int) -> str`: Find a match for the named player at the given current time, using the widening tiers above. Returns the matched opponent's name (both players are removed from the queue), or an empty string if no match is found.
- `queueSize() -> int`: Return the current number of players in the matchmaking queue.
Example 1:
```text
Input:
["Matchmaker", "addPlayer", "addPlayer", "addPlayer", "addPlayer", "findMatch", "removePlayer", "findMatch", "queueSize", "findMatch", "queueSize"]
[[], ["Ana", 1500, "NA", 0], ["Ben", 1580, "NA", 2], ["Cy", 1450, "EU", 0], ["Dee", 1420, "NA", 1], ["Ana", 3], ["Ana"], ["Ben", 10], [], ["Cy", 15], []]
Output: [null, null, null, null, null, "Dee", false, "", 2, "Ben", 0]
Explanation:
mm = Matchmaker()
mm.addPlayer("Ana", 1500, "NA", 0) # queue: {Ana}
mm.addPlayer("Ben", 1580, "NA", 2) # queue: {Ana, Ben}
mm.addPlayer("Cy", 1450, "EU", 0) # queue: {Ana, Ben, Cy}
mm.addPlayer("Dee", 1420, "NA", 1) # queue: {Ana, Ben, Cy, Dee}
mm.findMatch("Ana", 3) # -> "Dee" wait=3, tolerance 100, NA only. Cy (diff 50) is in EU.
# Ben and Dee both have diff 80; Dee enqueued earlier (1 < 2).
mm.removePlayer("Ana") # -> false Ana left the queue when she was matched
mm.findMatch("Ben", 10) # -> "" wait=8, tolerance 300, NA only, and no other NA player is queued
mm.queueSize() # -> 2 {Ben, Cy}
mm.findMatch("Cy", 15) # -> "Ben" wait=15 is the third tier: tolerance 500, any region, diff 130
mm.queueSize() # -> 0
```
Example 2:
```text
Input:
["Matchmaker", "addPlayer", "addPlayer", "addPlayer", "findMatch", "findMatch", "queueSize"]
[[], ["Dan", 1000, "EU", 0], ["Eve", 1200, "NA", 0], ["Frank", 1050, "EU", 1], ["Dan", 35], ["Eve", 36], []]
Output: [null, null, null, null, "Frank", "", 1]
Explanation:
mm = Matchmaker()
mm.addPlayer("Dan", 1000, "EU", 0) # queue: {Dan}
mm.addPlayer("Eve", 1200, "NA", 0) # queue: {Dan, Eve}
mm.addPlayer("Frank", 1050, "EU", 1) # queue: {Dan, Eve, Frank}
mm.findMatch("Dan", 35) # -> "Frank" wait=35, tolerance 1000, any region; Frank diff 50 beats Eve diff 200
mm.findMatch("Eve", 36) # -> "" wait=36, tolerance 1000, but Eve is the only player left
mm.queueSize() # -> 1 only Eve remains
```
```hint Map the wait first
Turn the requester's wait time into a tolerance and a region rule before you look at any candidate. Watch the edges: a wait of exactly 5, 15 or 30 seconds already belongs to the next tier.
```
```hint One ordering for the tie-break
The three tie-break rules describe a single ordering. Express it as one comparison key so choosing between two candidates is a single comparison.
```
### Constraints & Assumptions
- `1 <= skill <= 5000`
- Regions are non-empty strings.
- Player names are unique non-empty strings.
- `0 <= enqueue_time <= current_time <= 10^6`
- At most `10^4` calls total across all methods.
### Clarifying Questions to Ask
- What should `findMatch` return when the named player is not in the queue?
- Is a skill difference exactly equal to the tolerance allowed?
- Does the tolerance come only from the requesting player's wait time, or should the candidate's own wait time also limit the match?
- If `findMatch` finds nobody, does the requesting player stay in the queue?
### What a Strong Answer Covers
- A queue keyed by name, so add, remove and size run in constant time.
- The mapping from wait time to tolerance and region rule, with correct boundaries at 5, 15 and 30 seconds and an inclusive skill window.
- Candidate filtering that skips the requester and applies the region rule and the skill window.
- A deterministic choice by skill difference, then enqueue time, then name, with both players removed only when a match is found.
- The linear cost of `findMatch` and how an index by region and skill could reduce it.
### Follow-up Questions
- How would you index the queue by region and skill so `findMatch` does not scan every player?
- The rule is asymmetric: a player who has waited 30 seconds can pull in someone who just joined at a skill gap the newcomer's own tier would reject. How would you make matching fair to both sides?
- How would you run matching continuously in the background instead of only when a player calls `findMatch`?
- How would you extend this to parties, or to teams of more than two players?
Overview: Implement a matchmaker class that pairs queued players by skill and region and widens the allowed skill gap the longer a player waits. It tests mapping wait time to tiers with exact boundaries, filtering candidates, a three-level deterministic tie-break, and keeping the queue consistent after every match.