Snake and Ladder Game
Snake and Ladder is a compact LLD problem testing game board modeling, player movement, jump rules, win detection, and turn management — common as a lighter interview or warm-up.
Introduction
Snake and Ladder is a compact LLD problem testing game board modeling, player movement, jump rules, win detection, and turn management — common as a lighter interview or warm-up.
Classes: GameBoard, Player, Dice, SnakeAndLadderGame (or GameController). Board holds size and jump map; players take turns rolling dice until one reaches exactly the last cell.
Extensions: multiple dice, two-player vs n-player, bi-directional snakes.
Understanding the topic
Key concepts
- Board: size N, map from cell to destination (snake or ladder).
- Player: name, current position.
- Dice: random 1–6 (injectable for tests).
- Win: exact landing on size required.
- Turn order rotation among players.
- Immutable board config after game start.
Step-by-step explanation
- Initialize board with snakes and ladders.
- Register players at position 1.
- Active player rolls dice.
- Advance position; apply jump if mapped.
- Check win; else next player.
- Reject move overshooting final cell (optional rule).
Informative example
Full game with board, players, dice, and turn loop:
public interface Dice {int roll();}public final class RandomDice implements Dice {private final Random random = new Random();@Override public int roll() { return random.nextInt(6) + 1; }}public final class Player {private final String name;private int position;public Player(String name) {this.name = name;this.position = 1;}public String name() { return name; }public int position() { return position; }void moveTo(int cell) { position = cell; }}public final class GameBoard {private final int size;private final Map<Integer, Integer> jumps;public GameBoard(int size, Map<Integer, Integer> jumps) {this.size = size;this.jumps = Map.copyOf(jumps);}public int size() { return size; }public int resolve(int from, int steps) {int target = from + steps;if (target > size) return from;return jumps.getOrDefault(target, target);}}public final class SnakeAndLadderGame {private final GameBoard board;private final List<Player> players;private final Dice dice;private int turnIndex;public SnakeAndLadderGame(GameBoard board, List<Player> players, Dice dice) {this.board = board;this.players = List.copyOf(players);this.dice = dice;}public Optional<Player> playTurn() {Player current = players.get(turnIndex);int roll = dice.roll();int next = board.resolve(current.position(), roll);current.moveTo(next);turnIndex = (turnIndex + 1) % players.size();if (next == board.size()) return Optional.of(current);return Optional.empty();}}
FixedDice implementation enables deterministic tests; Builder configures snakes/ladders on board.
Real-world use
Real-world applications
- Lightweight OOD warm-up interview.
- Teaching state and turn management.
- Practice simple class collaboration.
Best practices
- Inject Dice for testability.
- Immutable jump map on board.
- Exact win rule configurable.
- Separate board config from game session.
- Log each turn for replay/debug.
Common mistakes
- Allowing move past final cell without bounce-back rule clarity.
- Snake and ladder on same cell undefined behavior.
- Mutating shared board mid-game.
- No turn order — simultaneous moves.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionCore snake and ladder classes?+
Answer
2BeginnerQuestionApply snake after move?+
Answer
3IntermediateQuestionExact win rule?+
Answer
4IntermediateQuestionTwo snakes same cell?+
Answer
5AdvancedQuestionAdd undo last move.+
Answer
Summary
Board holds size and jump mappings. Players rotate turns rolling dice. resolve applies movement and snakes/ladders. Win when landing exactly on final cell. Inject Dice for deterministic tests.