Design Snake and Ladder: Low-Level Design in JavaScript
772
Apr 01, 2026
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) asstart → endjumps. - 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
| Class | Responsibility |
|---|---|
Board | Map<start, end> for jumps; reject duplicate starts; getFinalPosition(pos) with cycle guard |
Dice | roll() — injectable for tests |
Player | Name + mutable position (starts at 0 = before cell 1) |
Game | Player order, takeTurn, winner, optional play() loop |
Approach
getFinalPosition(p): whilemap.has(p), setp = map.get(p); trackvisitedset to break infinite loops.takeTurn: roll → ifpos + roll > Nskip move → elsepos += roll→pos = board.getFinalPosition(pos)→ ifpos === Ndeclare winner → if dice is6, 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 againJump 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
Gameclass outline or yourgetFinalPositionin 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)

