Snake and Ladder

Classic Board Game

Design the board game: a numbered board with snakes and ladders, a die, players who take turns, and a win rule. The interviewer watches how you split rules into classes and how you test code that rolls dice.

Overview

Players start off the board and race to the last cell. On a turn a player rolls a die and moves forward that many cells. Some cells are the head of a snake: land there and you slide down to its tail. Some are the foot of a ladder: land there and you climb to its top. The first player to reach the last cell wins.

The game is small, which is the point. Interviewers use it to see whether you can turn a page of rules into a handful of classes with one job each, keep the rules configurable instead of hard-coded, and write code that can be tested even though it depends on a random die. A strong answer names the classes in the first two minutes, draws the turn as a flow, and shows a scripted die before writing the game loop.

Scoping the problem

The rules vary from one family to the next, so ask before you design. Every answer below changes one line of code, not the shape of the design.

  • How many cells, and how many dice faces? Both become constructor arguments, not constants. The usual answer is 100 cells and one six-sided die.
  • Does rolling the highest face give another turn? This decides whether the turn queue rotates after that roll. Usually yes, behind a flag.
  • What if the roll goes past the last cell? The player can stay put, bounce back, or win anyway. Usually the player stays put and must land exactly.
  • Can a ladder land on a snake, or a snake on a ladder? Chained jumps need a loop check. Usually no, and the board rejects such a setup.
  • How many players? Two or more, with the turn order kept as a queue. Usually 2 to 4.
  • Who supplies the randomness? A die you can script makes the game testable. The die takes a random generator you can seed or replace.

Functional requirements

  • Players start off the board (position 0) and move toward the last cell
  • A turn is one roll of a die with a configurable number of faces; the player moves forward that many cells
  • Landing on a snake head moves the player to its tail; landing on a ladder foot moves the player to its top
  • A roll that would pass the last cell leaves the player where they are
  • Rolling the highest face gives the same player another turn (a flag; on by default)
  • The first player to land exactly on the last cell wins and the game ends
  • Two or more players take turns in a fixed order

Non-functional requirements

  • A move costs one dictionary lookup, whatever the number of snakes and ladders
  • An invalid board (a snake going up, two jumps on one cell, a jump off the board) is rejected at setup with a clear error
  • Board size, dice faces and the extra-turn rule are constructor arguments, not constants
  • The die is injectable so a game can be replayed from a seed or scripted in tests

Interview questions

  • Why keep snakes and ladders in one dict instead of two lists?
  • How would you add special cells, like a cell that skips your next turn or doubles your roll?
  • Why a deque for turn order rather than an index into a list?
  • How do you test code that depends on a random die?
  • What if a ladder ends on a snake head? Should the player slide again?
  • Does this need thread safety?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio