Snake and Ladder is a strong rules + simulation problem: jumps, exact finish, dice, and often an extra turn on 6 plus edge cases like overshoot, chained ladders, and cycles.

More LLD & system design: cracksystemdesign.com

Challenge yourself: read through, then post your approach or code in the comments.


Clarify Before You Code

  • Board size N (classic: 100)?
  • Exact landing on N does overshoot mean no move?
  • Snake/ladder chains keep following until stable?
  • Cycles in jump graph detect and break?
  • Extra turn on 6 still apply if overshoot blocked? (Align with interviewer.)

Problem Statement

  • Cells 1..N; snakes (head > tail) and ladders (bottom < top) as start → end jumps.
  • 2+ players, round-robin turns.
  • Dice uniform in [1, 6] (or configurable).
  • Move: roll → if pos + roll > N, typically no move → else advance → apply jump chain from landing cell.
  • Win: land exactly on N.

Class Design

ClassResponsibility
BoardMap<start, end> for jumps; reject duplicate starts; getFinalPosition(pos) with cycle guard
Diceroll() — injectable for tests
PlayerName + mutable position (starts at 0 = before cell 1)
GamePlayer order, takeTurn, winner, optional play() loop

Approach

  1. getFinalPosition(p): while map.has(p), set p = map.get(p); track visited set to break infinite loops.
  2. takeTurn: roll → if pos + roll > N skip move → else pos += roll → pos = board.getFinalPosition(pos) → if pos === N declare winner → if dice is 6, same player rolls again (if that is your rule).

Flowcharts

One Turn (ASCII)

Roll dice
    |
    v
pos + roll > N?
   /         \
 YES          NO
  |            |
No move    pos += roll
             |
             v
      getFinalPosition(pos)
             |
             v
         pos == N?
          /      \
        YES       NO
         |         |
       WINNER   Rolled 6?
                /       \
              YES         NO
               |           |
          Same player   Next player
          rolls again

Jump Chain — getFinalPosition (ASCII)

Land on cell k
      |
      v
  jump from k?
   /         \
  NO          YES
   |           |
 Stop    already visited?
          /         \
        YES           NO
         |             |
    Cycle — break   move to dest
                        |
                        v
                  (repeat from top)

JavaScript Implementation

Resolve Jumps Safely

getFinalPosition(start) {
  let p = start;
  const seen = new Set();
  while (this.jumps.has(p)) {
    const next = this.jumps.get(p);
    if (seen.has(next)) break;   // cycle guard
    seen.add(p);
    p = next;
  }
  return p;
}

takeTurn Skeleton

takeTurn(player, boardSize) {
  const roll = this.dice.roll();
  if (player.position + roll > boardSize) {
    return this.handleExtraTurn(roll, false);  // overshoot — no move
  }
  player.position += roll;
  player.position = this.board.getFinalPosition(player.position);
  if (player.position === boardSize) {
    this.winner = player;
    return false;
  }
  return this.handleExtraTurn(roll, true);
}

handleExtraTurn(roll, moved) {
  return moved && roll === 6;  // true = same player rolls again
}

Complexity

  • Per turn: O(chain length); cycle guard makes it finite.
  • Space: O(S + L) for snake and ladder entries.

Extensions

  • Variable N, configurable bounce rules instead of no-move on overshoot.
  • Undo via command objects.

Discussion: Your Turn

1. Two snakes share the same head cell should Board throw at construction, or should last-write win? What do you tell the interviewer?

2. Write pseudocode for "extra turn on 6" when the player did not move due to overshoot which rule feels fair?

3. How would you unit-test getFinalPosition without random dice?

Paste your Game class outline or your getFinalPosition in the comments and compare with others.


Follow Crack System Design

  • Website: cracksystemdesign.com follow for new LLD articles.
  • We are assembling a JavaScript LLD practice repo (runnable games, clean class boundaries). Watch the site or follow me on leetcode for releases.
Comments (1)