Design Snake & Ladder
In a 45-minute LLD interview, the interviewer tests boundary condition management (exact-win overshoot logic, cycle prevention in obstacle layouts), deterministic testability (eliminating unmockable rand() calls via Strategy), and creational validation (verifying directional invariants like head > tail for snakes before board instantiation). Scope the solution to an arbitrary board dimension (e.g. 100 cells), pluggable dice rolling (DiceStrategy), obstacle mapping, and multiplayer FIFO rotation. Mention recursive obstacle chaining (springs/trampolines) and consecutive-roll bonus turns (rolling three 6s) as natural follow-up extensions.
Problem statement & clarifying requirements
Design an extensible Snake & Ladder game. The game must support configurable board sizes, custom placements of snakes and ladders created through a dedicated factory, pluggable dice rolling strategies (standard random 6-sided dice, deterministic sequences for testing, or crooked dice), and a clean turn-based multiplayer loop enforcing exact-win boundary rules.
Enforce the exact-win rule: if currentPos + roll > boardSize, the player cannot move and forfeits their turn (remains at currentPos).
For standard interview scope, jumpers do not chain recursively (a jumper destination is an ordinary cell). The factory must reject cycles and invalid bounds (head <= tail for snakes, bottom >= top for ladders).
Apply the Strategy pattern (DiceStrategy). Callers inject their preferred dice rolling implementation (StandardDice, DeterministicDice, CrookedDice) into the GameEngine.
All players start at position 0 (off-board), and their first valid roll moves them directly onto the board.
First, sketch the design yourself
Spend 5 minutes listing the classes, who owns whom, and which interface hides the part that changes.
- 1.
Encapsulate the creation and invariant validation of snakes and ladders inside a Factory Method (
JumperFactory) to prevent invalid states like upside-down snakes or negative positions. - 2.
Decouple random dice rolling using the Strategy pattern (
DiceStrategy). Never callstd::rand()or<random>directly insideGameEngine, as this prevents deterministic testing. - 3.
Use a fluent Builder pattern (
BoardBuilder) so clients can cleanly assemble boards with arbitrary dimensions and custom obstacle placements.
How to think through this problem (5-step walkthrough)
In a 45-minute LLD interview, the interviewer tests boundary condition management (exact-win overshoot logic, cycle prevention in obstacle layouts), deterministic testability (eliminating unmockable rand() calls via Strategy), and creational validation (verifying directional invariants like head > tail for snakes before board instantiation). Scope the solution to an arbitrary board dimension (e.g. 100 cells), pluggable dice rolling (DiceStrategy), obstacle mapping, and multiplayer FIFO rotation. Mention recursive obstacle chaining (springs/trampolines) and consecutive-roll bonus turns (rolling three 6s) as natural follow-up extensions.
Not every noun in the prompt deserves its own class. Separate dumb data structs from stateful managers, and turn trivial nouns into fields or enums.
Jumper / IBoardModifierObstacle model storing start, end, and type (SNAKE, LADDER). In simpler designs it serves as a pure value struct; when extended with dynamic obstacles (springs, teleporters, freeze traps), it acts as an IBoardModifier strategy interface.
GameBoardEncapsulates board dimension and an O(1) jumper destination map (std::unordered_map<int, int> jumpers_), computing destination cells and enforcing exact-win overshoot boundaries.
DiceStrategy / IDiceStrategy interface declaring roll(): int. Decouples game rules from random number generation, enabling StandardDice (randomized distribution), DeterministicDice (canned sequence for unit tests), and CrookedDice (custom weighted roll).
PlayerRepresents an active participant with display name, unique identifier, and current board position integer.
GameEngineOrchestrates the turn loop, manages the player rotation queue (std::deque<Player>), invokes DiceStrategy, requests destination calculation from GameBoard, and detects winning states.
SquareColor / JumpResultTrap noun β beginners often introduce dedicated SquareColor, BoardTile, or JumpResult classes. Visual tile colors belong in rendering/UI layers, and a jump result is simply a computed destination integer returned by GameBoard::getDestination(), requiring no persistent class.
A beginner writes a single loop inside main() with a hardcoded int board[101] array and calls rand() % 6 + 1 directly on every roll. Testing edge cases (landing on a snake, climbing a ladder, exact-win overshoot) becomes non-deterministic and impossible to automate. Furthermore, without a creational factory or builder, invalid obstacle configurations (like upside-down snakes where head <= tail or cycle loops 14 -> 28 -> 14) corrupt the board state and trigger infinite loops during play.
Calling std::rand() or <random> inside the game engine couples logic to system randomness, making unit testing and game replay verification impossible.
Scattering boundary and directional checks across board setup creates duplicated validation and allows malformed jumpers into the system.
Passing dozens of snake and ladder coordinate pairs into a single monolithic GameBoard constructor leads to unwieldy, error-prone signatures.
- β
Jumper directional invariant: Every snake must satisfy
head > tail, every ladder must satisfybottom < top, and no jumper can originate at position 0 (start) or the final winning position (boardSize). - β
Exact-win boundary invariant: If
currentPos + roll > boardSize, the move is invalid; the player remains atcurrentPosand forfeits their turn. - β
Ownership boundaries:
GameEngineuniquely ownsGameBoardandDiceStrategyviastd::unique_ptr.GameBoardowns obstacle mapping without coupling to player state or turn rotation.
Entities & design patterns used
Unlock Full UML Diagram & C++23 Solution
The complete UML diagram, production-ready C++23 code, follow-up extensions, and design pitfall analysis are part of Brute2Best Pro. All 7 foundation chapters are 100% free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.