Design Snake and Ladder (Low-Level Design)
A board game where the board, the players and the rules for a turn are separate pieces. Focus: Separating the board, the players and the turn rules.
| Difficulty | EASY |
|---|---|
| Design focus | Separating the board, the players and the turn rules |
| Automated grading | 3 public + 9 hidden tests (12 total) |
| Phase 2 follow-up | Sixes |
| Language | TypeScript (OOP / Machine Coding) |
Problem statement
Squares run from 1 to `size`. All players start at 0, off the board, and take turns in the order given.
A roll moves the player forward. If that would pass `size`, the player stays put. Landing on a snake's head moves the player down to its tail; landing at the bottom of a ladder moves them up to its top (one jump only).
The first player to land exactly on `size` wins and the game ends.
Functional requirements
- new SnakeAndLadder(size, snakes, ladders, players): snakes map head → tail (head above tail), ladders map bottom → top (top above bottom). Heads, tails and ladder bottoms are in 1..size-1; a ladder may end on size. Invalid boards throw.
- move(roll) plays the current player's turn and returns { player, from, to, won }. Rolls must be whole numbers 1 to 6. Moving after the game ended throws.
- position(player), currentPlayer(), winner() (or null).
Required API & starter interface
Implement the TypeScript class interface below. You can add any helper classes, policies or strategies you need:
class SnakeAndLadder {
constructor(
size: number,
snakes: Record<number, number>,
ladders: Record<number, number>,
players: string[]
) {}
move(roll: number): { player: string; from: number; to: number; won: boolean } {
throw new Error("Not implemented");
}
position(player: string): number {
throw new Error("Not implemented");
}
currentPlayer(): string {
throw new Error("Not implemented");
}
winner(): string | null {
throw new Error("Not implemented");
}
}
Public test cases
Your code runs against 3 public tests plus 9 hidden edge-case tests in a sandbox:
Players take turns
var g = new SnakeAndLadder(20, {}, {}, ["ann", "bob"]);
assertEqual(g.move(3), { player: "ann", from: 0, to: 3, won: false });
assertEqual(g.currentPlayer(), "bob");
g.move(2);
assertEqual(g.position("bob"), 2);
Snakes and ladders move you
var g = new SnakeAndLadder(20, { 7: 2 }, { 4: 15 }, ["ann", "bob"]);
assertEqual(g.move(4).to, 15);
assertEqual(g.move(5).to, 5);
Rolls must be 1 to 6
var g = new SnakeAndLadder(20, {}, {}, ["ann"]);
assertThrows(function () { g.move(7); });
assertThrows(function () { g.move(0); });
Design & structure checks
Beyond passing tests, the grader inspects your class structure for clean object-oriented design:
- The board is its own class and knows nothing about players or turns: Squares, snakes and ladders are the same whoever plays. A separate board can be tested alone and reused for other rule sets.
Phase 2: Change request
In real interviews, the follow-up tests whether your design is extensible. After your first version passes all 12 tests, you unlock the change request: Sixes.
Relevant low-level design lessons
From a problem to classes
Turn a problem statement into a first list of classes and methods.
Hide state behind methods
Protect an object's rules by keeping its data private.
One class, one job
Spot a class that does too much and split it.
More low-level design problems
Design Vending Machine
Take coins, sell products, give change, and refuse what a state does not allow.
Design Library System
Lend books to students and faculty with different limits, and charge fines for late returns.
Design Shopping Cart
Already solved, with comments: add items, apply tier discounts, then coupons. Run it and submit it to see how grading works.
Design Parking Lot
Park vehicles in the right size of spot, issue tickets and charge on exit.