Course contents

LLD problem — Snake and Ladder

Sign in to run this lab and save your progress

Learn

Before you start

Snake and Ladder is a turn-management + rule-checking exercise. Small enough for one interview slot. Enough detail to reveal design choices around game state.

The interesting decisions

  1. Turn order. How do you know who's next? Rotate an index, or store a next-player pointer? Both work; argue which fits.

  2. When does the turn ADVANCE? After every roll (including no-move overshoots)? Only on successful moves? What about after a win? Standard rule: rotate after every roll except after a win.

  3. Snake / ladder resolution. The from field in the roll result: is it the position BEFORE the roll, or the head/bottom you landed on? The tests pin from = pre-move position (before the die was applied). Your design.md should say why — usually because callers want to see the full transition, not just the snake/ladder jump.

  4. Game-over state. One flag, or figure it out from the positions? An explicit flag is cheaper.

The pattern question

Is there a pattern from modules 1-3 that fits here? Think before answering:

  • State? — Game has two states (playing, ended). One boolean is enough. State pattern for two states is overkill.
  • Strategy? — Dice rolling? We take the value as input. Nothing to strategize.
  • Command? — Rolls as first-class objects for replay? Neat but out of scope.
  • Observer? — Notify a UI on turn change? Not needed for MVP.

If your answer is "no pattern fits, plain classes are right", say so and defend it. That's a valid, interview-quality answer.

The rules discipline

  • No player past 100 (enforced by the "overshoot = no move" rule)
  • All positions in 0..100 (follows from the rule above
    • starting at 0)
  • No mutation after winner (guard at the top of roll)

Each has a clear place to enforce it. Your design.md should name them.

Your task

Classic dice-roll board game. Multiple players race from square 1 to square 100. Snakes drop you down; ladders lift you up. Turn management + probabilistic movement.

Determinism note: the tests pass explicit dice rolls, so no randomness in the tests. Your Game.roll(die_value) takes the roll as input — no internal RNG.

The problem

A 100-square board with:

  • A dict snakes mapping head → tail (head > tail)
  • A dict ladders mapping bottom → top (bottom < top)
  • A list of players (strings, order matters — first plays first)

All players start at square 0 (off the board). On their turn:

  • Roll a die value (1..6, we hand you the value — no randomness).
  • Move forward by the die value.
  • If landing on a snake head → slide down to tail.
  • If landing on a ladder bottom → climb to top.
  • If landing exactly on 100 → win.
  • If moving would go past 100 → NO MOVE (player stays put).

Turns rotate through the player list unless someone won.

Requirement-gathering questions

  1. Rolling a 6 gives extra turn? — No, for MVP.
  2. Land on snake TAIL — go up? — No, snakes only trigger at head.
  3. Ladder TOP is landed on — go down? — No, ladders only trigger at bottom.
  4. Two players on same square? — Allowed. No collision.
  5. Winning by exact roll? — Yes. Overshooting is a no-move.

Design goals

  • Turn management is clean and testable in isolation.
  • Snakes and ladders are DATA, not code — swapping the board doesn't rewrite logic.
  • Invariants: no player past 100; player position always in 0..100; game state stops mutating after a winner.

Your task

Public API:

from main import Game

g = Game(
    players=["alice", "bob"],
    snakes={17: 7, 62: 19, 87: 24},
    ladders={4: 14, 9: 31, 20: 38},
)
g.current_player()   # "alice"
g.roll(6)            # returns dict:
                     #   {"player": "alice", "from": 0, "to": 6,
                     #    "reason": "moved" | "snake" | "ladder"
                     #             | "no_move" | "win"}
g.winner()           # None | "alice" | "bob" ...
g.position("alice")  # int

Roll after game ended raises ValueError.

Click Submit to run the tests.

Starter files

Preview only — open the lab to edit and run.

files_dir
04-snake-and-ladder-starter