Design a Snake and Ladder Game (Low-Level Design)

Quick Overview

Design a Snake and Ladder game class with a configurable board, chained snakes and ladders, exact-landing wins and a preset dice sequence for testing. It tests object modeling, turn logic, setup validation and keeping a badly built board from hanging a turn.

Design a Snake and Ladder Game (Low-Level Design)

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a Snake and Ladder game that simulates the classic board game. The board has a configurable size with numbered cells from `1` to `boardSize`. Snakes and ladders are placed on the board as jumps between cells. Players take turns rolling a dice and advancing their position. If a player lands on the head of a snake, they slide down to its tail. If a player lands on the bottom of a ladder, they climb up to its top. Jumps can chain (for example, a ladder leading to a snake). A player wins by landing **exactly** on the final cell. If a dice roll would move the player beyond the board, they stay in their current position. The game supports multiple players who take turns in the order they were added. Implement a class `SnakeAndLadderGame(boardSize: int)` with the following methods: - `addSnake(head: int, tail: int)`: Add a snake on the board from head to tail. The head must be greater than the tail. - `addLadder(start: int, end: int)`: Add a ladder on the board from start to end. The start must be less than the end. - `addPlayer(name: str, position: int)`: Add a player with the given name starting at the given position. - `setDiceValues(values: List[int])`: Set a deterministic sequence of dice values to be used in order (cycling if needed). - `playTurn() -> str`: Play one turn for the current player. Returns a string describing the roll, movement, and any snake or ladder encounters. - `getPlayerPosition(name: str) -> int`: Return the current position of the named player, or -1 if not found. - `getWinner() -> str`: Return the name of the winner, or an empty string if no winner yet. The description returned by `playTurn` is a list of parts joined with `"; "`: - `<name> rolled <value>` always comes first. - `<name> stays at <cell>` follows when the roll would go past the final cell. - Otherwise `<name> moved to <cell>` follows, then one `Climbs ladder, goes to <cell>` or `Bitten by snake, goes to <cell>` part for each jump taken. - `<name> wins!` ends the description when the player finishes the move on the final cell. After a win, `playTurn` returns `Game already won by <name>`. With no players it returns `No players`. For example, on a 10-cell board with a ladder from 3 to 8, a snake from 8 to 5, and two players starting at 0: ```text game = SnakeAndLadderGame(10) game.addLadder(3, 8) game.addSnake(8, 5) game.addPlayer("Ana", 0) game.addPlayer("Ben", 0) game.setDiceValues([3, 2, 6, 5, 5]) game.playTurn() -> "Ana rolled 3; Ana moved to 3; Climbs ladder, goes to 8; Bitten by snake, goes to 5" game.playTurn() -> "Ben rolled 2; Ben moved to 2" game.playTurn() -> "Ana rolled 6; Ana stays at 5" game.playTurn() -> "Ben rolled 5; Ben moved to 7" game.playTurn() -> "Ana rolled 5; Ana moved to 10; Ana wins!" game.playTurn() -> "Game already won by Ana" game.getPlayerPosition("Ben") -> 7 game.getWinner() -> "Ana" ``` Ana's first roll lands on the ladder at 3, whose top is the snake head at 8, so she ends the turn on 5. From 5 a roll of 6 would pass cell 10, so she stays, and her next roll of 5 lands on 10 exactly. ```hint Storing the jumps Snakes and ladders differ only in direction. Ask what a move needs to know about the cell it lands on, and pick one structure that answers that in constant time. ``` ```hint Chained jumps A loop that keeps following jumps handles chains. Check what that loop does when a snake's tail is a ladder's start and that ladder's top is the same snake's head. ``` ### Constraints & Assumptions - `1 <= boardSize <= 400` - `1 <= number of players <= 4` - A snake's head is greater than its tail, and a ladder's start is less than its end. - Snakes and ladders do not share the same starting cell. - Dice values are between `1` and `6`. ### Clarifying Questions to Ask - Does reaching the final cell by a ladder count as a win, and can a snake's head sit on the final cell? - What should a roll return before `setDiceValues` is called, or when it is given an empty list? - Should invalid setup (a cell off the board, two jumps from one cell, a snake and a ladder that form a loop) raise an error or be ignored? - Are player names unique, and can players join after the first turn? ### What a Strong Answer Covers - How the classes split the state (game, players, dice, jump storage) and why each piece lives where it does. - The full resolution of one turn: overshoot, exact landing, chained jumps, and when the turn passes to the next player. - Setup validation, including a snake and ladder pair that forms a loop and would hang a turn. - How the dice becomes deterministic for tests without a separate code path for real games. - The time and space cost of a turn, of setup and of a player lookup. ### Follow-up Questions - How would you add house rules such as an extra turn on a 6, or losing the turn after three 6s in a row? - How would you support two dice, or cells with other effects such as skipping a turn? - How would you compute the fewest rolls needed to reach the final cell from cell 1? - What changes if players play online and their moves arrive as requests to a server?

Overview: Design a Snake and Ladder game class with a configurable board, chained snakes and ladders, exact-landing wins and a preset dice sequence for testing. It tests object modeling, turn logic, setup validation and keeping a badly built board from hanging a turn.

PracHub logo
General
Nov 24, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a Snake and Ladder game that simulates the classic board game. The board has a configurable size with numbered cells from 1 to boardSize. Snakes and ladders are placed on the board as jumps between cells.

Players take turns rolling a dice and advancing their position. If a player lands on the head of a snake, they slide down to its tail. If a player lands on the bottom of a ladder, they climb up to its top. Jumps can chain (for example, a ladder leading to a snake).

A player wins by landing exactly on the final cell. If a dice roll would move the player beyond the board, they stay in their current position. The game supports multiple players who take turns in the order they were added.

Implement a class SnakeAndLadderGame(boardSize: int) with the following methods:

  • addSnake(head: int, tail: int) : Add a snake on the board from head to tail. The head must be greater than the tail.
  • addLadder(start: int, end: int) : Add a ladder on the board from start to end. The start must be less than the end.
  • addPlayer(name: str, position: int) : Add a player with the given name starting at the given position.
  • setDiceValues(values: List[int]) : Set a deterministic sequence of dice values to be used in order (cycling if needed).
  • playTurn() -> str : Play one turn for the current player. Returns a string describing the roll, movement, and any snake or ladder encounters.
  • getPlayerPosition(name: str) -> int : Return the current position of the named player, or -1 if not found.
  • getWinner() -> str : Return the name of the winner, or an empty string if no winner yet.

The description returned by playTurn is a list of parts joined with "; ":

  • <name> rolled <value> always comes first.
  • <name> stays at <cell> follows when the roll would go past the final cell.
  • Otherwise <name> moved to <cell> follows, then one Climbs ladder, goes to <cell> or Bitten by snake, goes to <cell> part for each jump taken.
  • <name> wins! ends the description when the player finishes the move on the final cell.

After a win, playTurn returns Game already won by <name>. With no players it returns No players.

For example, on a 10-cell board with a ladder from 3 to 8, a snake from 8 to 5, and two players starting at 0:

game = SnakeAndLadderGame(10)
game.addLadder(3, 8)
game.addSnake(8, 5)
game.addPlayer("Ana", 0)
game.addPlayer("Ben", 0)
game.setDiceValues([3, 2, 6, 5, 5])

game.playTurn()               -> "Ana rolled 3; Ana moved to 3; Climbs ladder, goes to 8; Bitten by snake, goes to 5"
game.playTurn()               -> "Ben rolled 2; Ben moved to 2"
game.playTurn()               -> "Ana rolled 6; Ana stays at 5"
game.playTurn()               -> "Ben rolled 5; Ben moved to 7"
game.playTurn()               -> "Ana rolled 5; Ana moved to 10; Ana wins!"
game.playTurn()               -> "Game already won by Ana"
game.getPlayerPosition("Ben") -> 7
game.getWinner()              -> "Ana"

Ana's first roll lands on the ladder at 3, whose top is the snake head at 8, so she ends the turn on 5. From 5 a roll of 6 would pass cell 10, so she stays, and her next roll of 5 lands on 10 exactly.

Constraints & Assumptions

  • 1 <= boardSize <= 400
  • 1 <= number of players <= 4
  • A snake's head is greater than its tail, and a ladder's start is less than its end.
  • Snakes and ladders do not share the same starting cell.
  • Dice values are between 1 and 6 .

Clarifying Questions to Ask Guidance

  • Does reaching the final cell by a ladder count as a win, and can a snake's head sit on the final cell?
  • What should a roll return before setDiceValues is called, or when it is given an empty list?
  • Should invalid setup (a cell off the board, two jumps from one cell, a snake and a ladder that form a loop) raise an error or be ignored?
  • Are player names unique, and can players join after the first turn?

What a Strong Answer Covers Guidance

  • How the classes split the state (game, players, dice, jump storage) and why each piece lives where it does.
  • The full resolution of one turn: overshoot, exact landing, chained jumps, and when the turn passes to the next player.
  • Setup validation, including a snake and ladder pair that forms a loop and would hang a turn.
  • How the dice becomes deterministic for tests without a separate code path for real games.
  • The time and space cost of a turn, of setup and of a player lookup.

Follow-up Questions Guidance

  • How would you add house rules such as an extra turn on a 6, or losing the turn after three 6s in a row?
  • How would you support two dice, or cells with other effects such as skipping a turn?
  • How would you compute the fewest rolls needed to reach the final cell from cell 1?
  • What changes if players play online and their moves arrive as requests to a server?