Create an online match-making algorithm that receives players and dispatches a call to a game server when it finds players with ratings close enough to play against each other.
MatchMaking class
addPlayer(player id)- Game server information available
getRating(player1, player2)startGame(player1, player2)
Threshold to define“close enough”is a constant.
这道题考察的是在线匹配系统的设计:每当新玩家加入时,需要根据玩家评分判断是否存在“足够接近”的对手,并在满足阈值时立即调用游戏服务开启对局。实现时通常需要维护一个等待队列或按 rating 分组的数据结构,快速找到与新玩家差值在阈值内的候选人;如果存在多个候选,可以按最先进入、最接近评分等规则进行配对。核心重点是如何在流式加入玩家的场景下,兼顾查找效率和配对逻辑的清晰性。
正文完