Implement a Game Lobby State Machine for Multiplayer Matches

Quick Overview

Implement a lobby class that moves a multiplayer match through waiting, ready, in-game and finished states as players join, leave and ready up. It tests guard checks that reject invalid calls without side effects, and a derived ready state that stays correct after every change to the roster.

Implement a Game Lobby State Machine for Multiplayer Matches

Company: Riotgames

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a game lobby state machine that manages the lifecycle of a multiplayer match. A lobby is created with a `minPlayers` and `maxPlayers` count and starts in the `WAITING` state. The lobby transitions through four states: `WAITING` (accepting players), `READY` (all current players have marked themselves ready and at least `minPlayers` are present), `IN_GAME` (match in progress), and `FINISHED` (match concluded). Each operation must respect the current state: players can only join or leave while in `WAITING` or `READY`, readying up is only allowed in `WAITING` or `READY`, starting requires the `READY` state, and finishing requires the `IN_GAME` state. Invalid operations must return `false` without mutating the lobby. Implement the following methods: - `joinPlayer(playerId: str) -> bool`: Add a player to the lobby. Returns `true` on success. Fails if the lobby is not in `WAITING` or `READY`, the player is already present, or the lobby is at `maxPlayers`. - `leavePlayer(playerId: str) -> bool`: Remove a player from the lobby. Returns `true` on success. Fails if the lobby is not in `WAITING` or `READY`, or the player is not present. - `readyUp(playerId: str) -> bool`: Mark a player as ready. Returns `true` on success. Fails if the lobby is not in `WAITING` or `READY`, or the player is not in the lobby. - `unready(playerId: str) -> bool`: Mark a player as not ready. Returns `true` on success. Fails if the lobby is not in `WAITING` or `READY`, or the player is not in the lobby or not currently ready. - `startGame() -> bool`: Transition from `READY` to `IN_GAME`. Returns `true` on success, `false` if the lobby is not in `READY`. - `finishGame() -> bool`: Transition from `IN_GAME` to `FINISHED`. Returns `true` on success, `false` if the lobby is not in `IN_GAME`. - `getState() -> str`: Return the current state of the lobby as one of `"WAITING"`, `"READY"`, `"IN_GAME"`, or `"FINISHED"`. Example 1: ```text Input: ["GameLobby", "joinPlayer", "readyUp", "getState", "joinPlayer", "readyUp", "getState", "joinPlayer", "startGame", "readyUp", "joinPlayer", "unready", "leavePlayer", "startGame", "getState"] [[2, 3], ["ana"], ["ana"], [], ["ben"], ["ben"], [], ["cal"], [], ["cal"], ["dan"], ["ben"], ["ben"], [], []] Output: [null, true, true, "WAITING", true, true, "READY", true, false, true, false, true, true, true, "IN_GAME"] Explanation: lobby = GameLobby(2, 3) # min=2, max=3 lobby.joinPlayer("ana") # true players {ana}, 1 < min, WAITING lobby.readyUp("ana") # true everyone is ready but only 1 player, WAITING lobby.getState() # "WAITING" lobby.joinPlayer("ben") # true ben is not ready, WAITING lobby.readyUp("ben") # true 2 players, both ready, READY lobby.getState() # "READY" lobby.joinPlayer("cal") # true cal is not ready, back to WAITING lobby.startGame() # false lobby is WAITING lobby.readyUp("cal") # true 3 players, all ready, READY lobby.joinPlayer("dan") # false lobby is full (max 3) lobby.unready("ben") # true ben is not ready, WAITING lobby.leavePlayer("ben") # true ana and cal remain, both ready, READY lobby.startGame() # true IN_GAME lobby.getState() # "IN_GAME" ``` Example 2: ```text Input: ["GameLobby", "joinPlayer", "readyUp", "getState", "startGame", "joinPlayer", "getState", "leavePlayer", "finishGame"] [[1, 2], ["zoe"], ["zoe"], [], [], ["max"], [], ["zoe"], []] Output: [null, true, true, "READY", true, false, "IN_GAME", false, true] Explanation: lobby = GameLobby(1, 2) # min=1, max=2 lobby.joinPlayer("zoe") # true players {zoe} lobby.readyUp("zoe") # true all ready, count in range, READY lobby.getState() # "READY" lobby.startGame() # true IN_GAME lobby.joinPlayer("max") # false cannot join while IN_GAME lobby.getState() # "IN_GAME" lobby.leavePlayer("zoe") # false cannot leave while IN_GAME lobby.finishGame() # true FINISHED ``` ```hint Where READY comes from No method is asked to set `WAITING` or `READY` directly. Work out which stored facts decide between the two, and which calls can change those facts. ``` ```hint Check before you change A failed call must leave the lobby untouched, so run every state and membership check before the first mutation. Then trace what a join does to a lobby that is already `READY`. ``` ### Constraints & Assumptions - `1 <= minPlayers <= maxPlayers <= 16` - Player IDs are unique non-empty strings of length up to 20. - At most `10^4` operations will be performed on a lobby. - Calls arrive one at a time. A single lobby is never accessed concurrently. ### Clarifying Questions to Ask - Should `readyUp` for a player who is already ready return `true` or `false`? - When a new player joins a `READY` lobby, does the lobby drop back to `WAITING`? - Is `FINISHED` terminal, or can a finished lobby be reset for another match? - If a player leaves and rejoins, do they keep their ready flag? ### What a Strong Answer Covers - A representation (player set, ready set, current state) with its invariants stated, including that the ready set is always a subset of the players. - State and membership checks that run before any mutation, so a rejected call changes nothing. - Automatic movement between `WAITING` and `READY` after every join, leave, readyUp and unready, including the cases where a join or a leave flips the state. - Explicit `startGame` and `finishGame` transitions, with `FINISHED` as a terminal state. - The cost of each operation and why it is constant. ### Follow-up Questions - How would you make one lobby safe when several server threads handle player requests at the same time? - How would you add a countdown that starts the match a few seconds after the lobby becomes `READY` and cancels if anyone unreadies or leaves? - How would you push every state change to the connected clients? - How would you add a host who can kick players, and what new checks would that need?

Overview: Implement a lobby class that moves a multiplayer match through waiting, ready, in-game and finished states as players join, leave and ready up. It tests guard checks that reject invalid calls without side effects, and a derived ready state that stays correct after every change to the roster.

Riotgames logo
Riotgames
Nov 24, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a game lobby state machine that manages the lifecycle of a multiplayer match. A lobby is created with a minPlayers and maxPlayers count and starts in the WAITING state.

The lobby transitions through four states: WAITING (accepting players), READY (all current players have marked themselves ready and at least minPlayers are present), IN_GAME (match in progress), and FINISHED (match concluded).

Each operation must respect the current state: players can only join or leave while in WAITING or READY, readying up is only allowed in WAITING or READY, starting requires the READY state, and finishing requires the IN_GAME state. Invalid operations must return false without mutating the lobby.

Implement the following methods:

  • joinPlayer(playerId: str) -> bool : Add a player to the lobby. Returns true on success. Fails if the lobby is not in WAITING or READY , the player is already present, or the lobby is at maxPlayers .
  • leavePlayer(playerId: str) -> bool : Remove a player from the lobby. Returns true on success. Fails if the lobby is not in WAITING or READY , or the player is not present.
  • readyUp(playerId: str) -> bool : Mark a player as ready. Returns true on success. Fails if the lobby is not in WAITING or READY , or the player is not in the lobby.
  • unready(playerId: str) -> bool : Mark a player as not ready. Returns true on success. Fails if the lobby is not in WAITING or READY , or the player is not in the lobby or not currently ready.
  • startGame() -> bool : Transition from READY to IN_GAME . Returns true on success, false if the lobby is not in READY .
  • finishGame() -> bool : Transition from IN_GAME to FINISHED . Returns true on success, false if the lobby is not in IN_GAME .
  • getState() -> str : Return the current state of the lobby as one of "WAITING" , "READY" , "IN_GAME" , or "FINISHED" .

Example 1:

Input:
["GameLobby", "joinPlayer", "readyUp", "getState", "joinPlayer", "readyUp", "getState", "joinPlayer", "startGame", "readyUp", "joinPlayer", "unready", "leavePlayer", "startGame", "getState"]
[[2, 3], ["ana"], ["ana"], [], ["ben"], ["ben"], [], ["cal"], [], ["cal"], ["dan"], ["ben"], ["ben"], [], []]

Output: [null, true, true, "WAITING", true, true, "READY", true, false, true, false, true, true, true, "IN_GAME"]

Explanation:
lobby = GameLobby(2, 3)        # min=2, max=3
lobby.joinPlayer("ana")        # true     players {ana}, 1 < min, WAITING
lobby.readyUp("ana")           # true     everyone is ready but only 1 player, WAITING
lobby.getState()               # "WAITING"
lobby.joinPlayer("ben")        # true     ben is not ready, WAITING
lobby.readyUp("ben")           # true     2 players, both ready, READY
lobby.getState()               # "READY"
lobby.joinPlayer("cal")        # true     cal is not ready, back to WAITING
lobby.startGame()              # false    lobby is WAITING
lobby.readyUp("cal")           # true     3 players, all ready, READY
lobby.joinPlayer("dan")        # false    lobby is full (max 3)
lobby.unready("ben")           # true     ben is not ready, WAITING
lobby.leavePlayer("ben")       # true     ana and cal remain, both ready, READY
lobby.startGame()              # true     IN_GAME
lobby.getState()               # "IN_GAME"

Example 2:

Input:
["GameLobby", "joinPlayer", "readyUp", "getState", "startGame", "joinPlayer", "getState", "leavePlayer", "finishGame"]
[[1, 2],      ["zoe"],      ["zoe"],   [],         [],          ["max"],      [],         ["zoe"],       []]

Output: [null, true, true, "READY", true, false, "IN_GAME", false, true]

Explanation:
lobby = GameLobby(1, 2)        # min=1, max=2
lobby.joinPlayer("zoe")        # true     players {zoe}
lobby.readyUp("zoe")           # true     all ready, count in range, READY
lobby.getState()               # "READY"
lobby.startGame()              # true     IN_GAME
lobby.joinPlayer("max")        # false    cannot join while IN_GAME
lobby.getState()               # "IN_GAME"
lobby.leavePlayer("zoe")       # false    cannot leave while IN_GAME
lobby.finishGame()             # true     FINISHED

Constraints & Assumptions

  • 1 <= minPlayers <= maxPlayers <= 16
  • Player IDs are unique non-empty strings of length up to 20.
  • At most 10^4 operations will be performed on a lobby.
  • Calls arrive one at a time. A single lobby is never accessed concurrently.

Clarifying Questions to Ask Guidance

  • Should readyUp for a player who is already ready return true or false ?
  • When a new player joins a READY lobby, does the lobby drop back to WAITING ?
  • Is FINISHED terminal, or can a finished lobby be reset for another match?
  • If a player leaves and rejoins, do they keep their ready flag?

What a Strong Answer Covers Guidance

  • A representation (player set, ready set, current state) with its invariants stated, including that the ready set is always a subset of the players.
  • State and membership checks that run before any mutation, so a rejected call changes nothing.
  • Automatic movement between WAITING and READY after every join, leave, readyUp and unready, including the cases where a join or a leave flips the state.
  • Explicit startGame and finishGame transitions, with FINISHED as a terminal state.
  • The cost of each operation and why it is constant.

Follow-up Questions Guidance

  • How would you make one lobby safe when several server threads handle player requests at the same time?
  • How would you add a countdown that starts the match a few seconds after the lobby becomes READY and cancels if anyone unreadies or leaves?
  • How would you push every state change to the connected clients?
  • How would you add a host who can kick players, and what new checks would that need?